Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 400617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:57:28+00:00 2026-05-12T16:57:28+00:00

Hello Functional C# Friends, So this time i am trying to compact my code

  • 0

Hello Functional C# Friends,

So this time i am trying to compact my code and write in more functional , lambda style, as well as i would like to avaoid creating unneccessary lists and classes and let compiler do the work for me. I did manage to convert some small piece of code in functional way but after that i dont have much idea as how to go about.

var errorList = new List<DataRow>();

IEnumerable<DataRow> resultRows = GetResultRows();

resultRows      
     .Filter(row => row.Field<string>("status").Equals("FAILURE", StringComparison.InvariantCultureIgnoreCase))
     .ForEach(row => { errorList.Add(row); });

if (errorList.Count > 0)
{
    var excludedBooks = new List<string>();
    foreach (DataRow row in errorList)
    {
        if (ToUseBooksList.Contains((string)row["main_book"]))
        {
            BookCheckResults.AddRow(string.Format("Error for MainBook {0}, RiskType {1}",
                                                  row["main_book"], row["risk_type"]));
            if (!excludedBooks.Contains((string)row["main_book"]))
            {
                excludedBooks.Add((string)row["main_book"]);
            }
        }
    }
}

My Extension methods :

public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
{
    if (collection == null)
        throw new ArgumentNullException("collection");
    if (action == null)
        throw new ArgumentNullException("action");

    foreach (var item in collection)
        action(item);
}

public static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Predicate<T> func)
{
    foreach (var item in source)
    {
        if (func(item))
            yield return item;
    }
}

I will highly appreciate if you can help me structing this code is more functional, lambda style.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:57:28+00:00Added an answer on May 12, 2026 at 4:57 pm

    The ForEach extension method usually isn’t worth the bother.

    Eric Lippert has blogged about it, and his philosophical objection to it is that it looks like a side-effect free expression (like most Linq features) but it is actually a side-effecting imperative statement in disguise.

    If you want to carry out an action for each item on a list, use the foreach statement. That’s what it’s for.

    If you want to manipulate lists of actions, then you can do that, but then you want IEnumerable<Action>.

    For the first part of your code, how about:

    var errorList = GetResultRows().Where(row => row.Field<string>("status").Equals("FAILURE", StringComparison.InvariantCultureIgnoreCase)
                                   .ToList();
    

    You have a List<string> called excluded books. Use HashSet<string> instead, and you don’t need to check if a string is already added to it:

    var excludedBooks = new HashSet<string>();
    foreach (DataRow row in errorList)
    {
        if (ToUseBooksList.Contains((string)row["main_book"]))
        {
            BookCheckResults.AddRow(string.Format("Error for MainBook {0}, RiskType {1}",
                                                  row["main_book"], row["risk_type"]));
    
            excludedBooks.Add((string)row["main_book"]);
        }
    }
    

    You can also filter the list with Where:

    var excludedBooks = new HashSet<string>();
    foreach (DataRow row in errorList.Where(r => ToUseBooksList.Contains((string)r["main_book"]))
    {
        BookCheckResults.AddRow(string.Format("Error for MainBook {0}, RiskType {1}",
                                                  row["main_book"], row["risk_type"]));
    
        excludedBooks.Add((string)row["main_book"]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Hello friends i am running code given below which contains the setLogTimeEntery function and
I'm looking for a concise and functional style way to apply a function to
Hello world. × jQuery $('#closeerrordiv').click(function() { $(this).parent().animate({opacity: 0}, 'slow', function() { $('#regWarnMsg').slideUp('slow'); }); });
Hello I need to generate a report which would include both data and a
I've inherited a Visual Studio 6.0 project to convert to 2005. It includes this
This PHP script prints all the data minus the XML to the browser (I'm
(edited from original post to change BaseMessage to const BaseMessage&) Hello All, I'm very
I have tried for three days now and gotten nowhere on this.... I absolutely
I'm learning jQuery and can't figure this out: Here's the HTML <a id=myid name=myid
Hello I am using jquery layout plugin from http://layout.jquery-dev.net/ . my options are following:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.