Parallel iteration in C#?

前端 未结 6 1559
耶瑟儿~
耶瑟儿~ 2021-02-06 08:05

Is there a way to do foreach style iteration over parallel enumerables in C#? For subscriptable lists, I know one could use a regular for loop iterati

6条回答
  •  旧时难觅i
    2021-02-06 08:24

    If you want to stick to the basics - I rewrote the currently accepted answer in a simpler way:

        public static IEnumerable Combine (this IEnumerable> sources)
        {
            var enums = sources
                .Select (s => s.GetEnumerator ())
                .ToArray ();
    
            while (enums.All (e => e.MoveNext ())) {
                yield return enums.Select (e => e.Current).ToArray ();
            }
        }
    
        public static IEnumerable Combine (params IEnumerable[] sources)
        {
            return sources.Combine ();
        }
    

提交回复
热议问题