Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I'm understanding the question correctly, you want to join multiple sequences based on their position within the sequence?</p> <p>There isn't anything in the <code>System.Linq.Enumerable</code> class to do this as both the <code>Join</code> and <code>GroupJoin</code> methods are based on join keys. However, by coincidence I wrote a <code>PositionalJoin</code> method for just this purpose a few days back, used as in your example:</p> <pre><code>sequenceA.PositionalJoin(sequenceB, (a, b) =&gt; new { a, b }); </code></pre> <p>The semantics of the method shown below is that it does not require the sequences to be of equal length, but it would be trivial to modify it to require this. I also commented out where the argument checking should be as it was using our internal helper classes.</p> <pre><code>public static IEnumerable&lt;TResult&gt; PositionalJoin&lt;T1, T2, TResult&gt;( this IEnumerable&lt;T1&gt; source1, IEnumerable&lt;T2&gt; source2, Func&lt;T1, T2, int, TResult&gt; selector) { // argument checking here return PositionalJoinIterator(source1, source2, selector); } private static IEnumerable&lt;TResult&gt; PositionalJoinIterator&lt;T1, T2, TResult&gt;( IEnumerable&lt;T1&gt; source1, IEnumerable&lt;T2&gt; source2, Func&lt;T1, T2, TResult&gt; selector) { using (var enumerator1 = source1.GetEnumerator()) using (var enumerator2 = source2.GetEnumerator()) { bool gotItem; do { gotItem = false; T1 item1; if (enumerator1.MoveNext()) { item1 = enumerator1.Current; gotItem = true; } else { item1 = default(T1); } T2 item2; if (enumerator2.MoveNext()) { item2 = enumerator2.Current; gotItem = true; } else { item2 = default(T2); } if (gotItem) { yield return selector(item1, item2); } } while (gotItem); } } </code></pre> <p>Not sure if this is exactly what you're looking for, but hopefully of some help.</p>
 

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