[Test]
public void SplitWithParallel()
{
var list = new List<int>(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
list.Split(4).AsParallel().ForAll(x => x.ForEach(y => Console.WriteLine(y) /*or run a query*/));
}
public static class Extensions
{
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts)
{
int i = 0;
var splits = from item in list
group item by i++%parts
into part
select part.AsEnumerable();
return splits;
}
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
{
action(item);
}
}
}
Comments
Post a Comment