您可以滚动自己的ZipMany实例,该实例手动迭代每个枚举。与
GroupBy投影每个序列后使用的序列相比,这可能会在较大的序列上表现更好:
public static IEnumerable<TResult> ZipMany<TSource, TResult>( IEnumerable<IEnumerable<TSource>> source, Func<IEnumerable<TSource>, TResult> selector){ // ToList is necessary to avoid deferred execution var enumerators = source.Select(seq => seq.GetEnumerator()).ToList(); try { while (true) { foreach (var e in enumerators) {bool b = e.MoveNext();if (!b) yield break; } // Again, ToList (or ToArray) is necessary to avoid deferred execution yield return selector(enumerators.Select(e => e.Current).ToList()); } } finally { foreach (var e in enumerators) e.Dispose(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)