Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Remove unneccessary list using lambda and functional C# paradigm
    primarykey
    data
    text
    <p>Hello Functional C# Friends,</p> <p>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.</p> <pre><code>var errorList = new List&lt;DataRow&gt;(); IEnumerable&lt;DataRow&gt; resultRows = GetResultRows(); resultRows .Filter(row =&gt; row.Field&lt;string&gt;("status").Equals("FAILURE", StringComparison.InvariantCultureIgnoreCase)) .ForEach(row =&gt; { errorList.Add(row); }); if (errorList.Count &gt; 0) { var excludedBooks = new List&lt;string&gt;(); 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"]); } } } } </code></pre> <p>My Extension methods :</p> <pre><code>public static void ForEach&lt;T&gt;(this IEnumerable&lt;T&gt; collection, Action&lt;T&gt; 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&lt;T&gt; Filter&lt;T&gt;(this IEnumerable&lt;T&gt; source, Predicate&lt;T&gt; func) { foreach (var item in source) { if (func(item)) yield return item; } } </code></pre> <p>I will highly appreciate if you can help me structing this code is more functional, lambda style.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload