Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp converting foreach to Linq
    text
    copied!<p>this is a homework question.</p> <p>I am doing diffs on our domain model and I've set it up so that I can iterate a list of operations that check for certian differences within the domain. I pass in the differencing function and the before and after states of the object graph to produce a result in the DiffContext - which is used later to set up a payload for calling another service. But I've made some changes and need help with the Linq syntax</p> <p>So, I have the following code ...</p> <pre><code>public static IEnumerable&lt;DiffContext&gt; GetFirstDifference&lt;T&gt;(IEnumerable&lt;Func&lt;T, T, DiffContext&gt;&gt; diffOperations, T beforeState, T afterState) { return from op in diffOperations let diff = op(beforeState, afterState) where diff.FoundDifference select diff; } </code></pre> <p>Which I modified to use <code>Func&lt;T, T, IEnumerable&lt;DiffContext&gt;&gt;</code> instead of the previous <code>Func&lt;T, T, DiffContext&gt;</code> - because now my diff operations can return multiple differences. Like so..</p> <pre><code>public static IEnumerable&lt;DiffContext&gt; GetFirstDifference&lt;T&gt;(IEnumerable&lt;Func&lt;T, T, IEnumerable&lt;DiffContext&gt;&gt;&gt; diffOperations, T beforeState, T afterState) { foreach (var op in diffOperations) { foreach (var diff in op(beforeState, afterState)) { yield return diff; } } } </code></pre> <p>But now I have this nested foreach and I'd like some help converting it to the Linq equivalent. Can you help?</p> <p>Thanks Jon Skeet. I now have the following instead of the nested foreach:</p> <pre><code> return from op in diffOperations from diff in op(beforeState, afterState) where diff.FoundDifference select diff; </code></pre>
 

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