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?
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With