利用ExtensionMethods
Ex: 一次合併五個List
namespace ExtensionMethods
{
public static class MyExtensions
{
public static IEnumerable<TResult> ZipMultiList<T1, T2, T3, T4, T5 , TResult>(
this IEnumerable<T1> first,
IEnumerable<T2> second,
IEnumerable<T3> third,
IEnumerable<T4> fourth,
IEnumerable<T5> fifth,
Func<T1, T2, T3,T4,T5, TResult> resultSelector)
{
using (IEnumerator<T1> iterator1 = first.GetEnumerator())
using (IEnumerator<T2> iterator2 = second.GetEnumerator())
using (IEnumerator<T3> iterator3 = third.GetEnumerator())
using (IEnumerator<T4> iterator4 = fourth.GetEnumerator())
using (IEnumerator<T5> iterator5 = fifth.GetEnumerator())
{
while (iterator1.MoveNext() && iterator2.MoveNext() && iterator3.MoveNext() && iterator4.MoveNext() && iterator5.MoveNext() )
{
yield return resultSelector(iterator1.Current, iterator2.Current, iterator3.Current, iterator4.Current, iterator5.Current);
}
}
}
}
}
呼叫:
var output = list1.ZipMultiList( list2, list3, list4, list5,
(n1, n2, n3, n4, n5) => new { Cn = n1, On = n2, Pk = n3, Bf = n4, Pt=n5 }).ToList();