Appending/concatenating two IEnumerable sequences

后端 未结 4 1722
渐次进展
渐次进展 2020-12-15 15:01

I have two sets of datarows. They are each IEnumerable. I want to append/concatenate these two lists into one list. I\'m sure this is doable. I don\'t want to do a for loop

4条回答
  •  遥遥无期
    2020-12-15 16:08

    Delayed invocation of the second and subsequent enumerables

    I usually use Linq IEnumerable.Concat() but today I needed to be 100% sure that the second enumeration was not enumerated until the first one has been processed until the end. (e.g. two db queries that I didn't want to run simultaneously). So the following function made the trick to delay the enumerations.

        IEnumerable DelayedConcat(params Func>[] enumerableList)
        {
            foreach(var enumerable in enumerableList)
            {
                foreach (var item in enumerable())
                {
                    yield return item;
                }
            }
        }
    

    Usage:

        return DelayedConcat(
                    () => GetEnumerable1(),
                    () => GetEnumerable2(),
     // and so on.. () => GetEnumerable3(),
                    );
    

    In this example GetEnumerable2 function invocation will be delayed until GetEnumerable1 has been enumerated till the end.

提交回复
热议问题