Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Linq and C#, is it possible to join two lists but with interleaving at each item?

Tags:

c#

.net

linq

Having two lists of same object type. I want to join them using an interleave pattern where i items of the first list are separated by j items from the second list.

In essence:

First list

{a, b, c, d, e, f, g, h}

Second list

{0, 1, 2, 3, 4}

where the grouping count for the first list is 3 and for the second list is 2.

resulting in

{a, b, c, 0, 1, e, f, g, 2, 3, h, 4}

Is this possible with Linq?

like image 635
Stécy Avatar asked Dec 23 '22 07:12

Stécy


1 Answers

There's nothing within LINQ itself to do this - it seems a pretty specialized requirement - but it's fairly easy to implement:

public static IEnumerable<T> InterleaveWith<T>
   (this IEnumerable<T> first, IEnumerable<T> second,
    int firstGrouping, int secondGrouping)
{
    using (IEnumerator<T> firstIterator = first.GetEnumerator())
    using (IEnumerator<T> secondIterator = second.GetEnumerator())
    {
        bool exhaustedFirst = false;
        // Keep going while we've got elements in the first sequence.
        while (!exhaustedFirst)
        {                
            for (int i = 0; i < firstGrouping; i++)
            {
                 if (!firstIterator.MoveNext())
                 {
                     exhaustedFirst = true;
                     break;
                 }
                 yield return firstIterator.Current;
            }
            // This may not yield any results - the first sequence
            // could go on for much longer than the second. It does no
            // harm though; we can keep calling MoveNext() as often
            // as we want.
            for (int i = 0; i < secondGrouping; i++)
            {
                 // This is a bit ugly, but it works...
                 if (!secondIterator.MoveNext())
                 {
                     break;
                 }
                 yield return secondIterator.Current;
            }
        }
        // We may have elements in the second sequence left over.
        // Yield them all now.
        while (secondIterator.MoveNext())
        {
            yield return secondIterator.Current;
        }
    }
}
like image 167
Jon Skeet Avatar answered May 10 '23 05:05

Jon Skeet