Assume I have two (or more) IEnumerable<T>
with many elements. Every IEnumerable
has another type T
. The lists can be extreme long and should not be loaded to memory completetly.
IEnumerable<int> ints = getManyInts();
IEnumerable<string> strings = getSomeStrings();
IEnumerable<DateTime> dates = getSomeDates();
What I want to do is to iterate over these lists, and get an item containing one int, one string and one DateTime for each step, until the end of the longest or the shortest list has been reached. Both cases should be supported (bool param longest vs. shortest or so). For every item unavailable in the shorter lists (because the end has already been reached) I'd expect default values.
for(Tuple<int,string,DateTime> item in
Foo.Combine<int,string,DateTime>(ints, strings, dates))
{
int i=item.Item1;
string s=item.Item2;
DateTime d=item.Item3;
}
Is it possible to do this with linq using deferred execution? I know the solution using IEnumerators directly combined with yield return. See How can I iterate over two IEnumerables simultaneously in .NET 2
Something like this should do it (warning- untested):