Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update:</strong></p> <p>Eric Lippert recently posted on this: <a href="http://blogs.msdn.com/ericlippert/archive/2009/05/07/zip-me-up.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/ericlippert/archive/2009/05/07/zip-me-up.aspx</a></p> <p>It's especially interesting because he's posted the source for the new extension in C#4:</p> <pre><code>public static IEnumerable&lt;TResult&gt; Zip&lt;TFirst, TSecond, TResult&gt; (this IEnumerable&lt;TFirst&gt; first, IEnumerable&lt;TSecond&gt; second, Func&lt;TFirst, TSecond, TResult&gt; resultSelector) { if (first == null) throw new ArgumentNullException("first"); if (second == null) throw new ArgumentNullException("second"); if (resultSelector == null) throw new ArgumentNullException("resultSelector"); return ZipIterator(first, second, resultSelector); } private static IEnumerable&lt;TResult&gt; ZipIterator&lt;TFirst, TSecond, TResult&gt; (IEnumerable&lt;TFirst&gt; first, IEnumerable&lt;TSecond&gt; second, Func&lt;TFirst, TSecond, TResult&gt; resultSelector) { using (IEnumerator&lt;TFirst&gt; e1 = first.GetEnumerator()) using (IEnumerator&lt;TSecond&gt; e2 = second.GetEnumerator()) while (e1.MoveNext() &amp;&amp; e2.MoveNext()) yield return resultSelector(e1.Current, e2.Current); } </code></pre> <hr> <p><strong>Original answer:</strong></p> <p>Are you referring to a join?</p> <pre><code>from x in seq1 join y in seq2 on x.foo equals y.foo select new {x, y} </code></pre> <p>There is also pLinq - which executes linq statements in parallel (across multiple threads).</p> <hr> <p><strong>Edit:</strong></p> <p>Ah - thanks for clarifying the question, though I really don't think my answer deserved a vote down.</p> <p>It sounds like what you want is something like:</p> <pre><code>from x in seq1 join y in seq2 on x.Index equals y.Index select new {x.Foo, y.Bar} </code></pre> <p>Unfortunately you can't do that with Linq - it extends <code>IEnumerable</code>, which only really has <code>current</code> and <code>next</code> properties, so no index property.</p> <p>Obviously you can do this easily in C# with a nested for-loop and an if block, but you can't with Linq I'm afraid.</p> <p>The only way to mimic this in linq syntax is to artificially add the index:</p> <pre><code>int counter = 0; var indexed1 = ( from x in seq1 select { item = x, index = counter++ } ).ToList(); //note the .ToList forces execution, this won't work if lazy counter = 0; var indexed2 = ( from x in seq2 select { item = x, index = counter++ } ).ToList(); var result = from x in indexed1 join y in indexed2 on x.index = y.index select new {x.item.Foo, y.item.Bar} </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