Use this if you have a collection of objects, and you need to devide this collection into many parts.
This is an example:
var testCollection = new List {"1", "2", "3", "4", "5", "6"};
var parts=testCollection.Devide(3);
for(int i=0;i<parts.Count();i++)
Console.WriteLine(string.Format("Part {0} -> {1}",i+1,string.Join(" - ",parts.ElementAt(i).ToArray())));
Output:
Part 1 -> 1 - 2
Part 2 -> 3 - 4
Part 3 -> 5 - 6
public static class EnumarableExtensions
{
public static IEnumerable<IEnumerable<T>> Devide<T>(this IEnumerable<T> items,
int partsNumber)
{
var parts = items.Partition(items.Count() / partsNumber).ToList();
var count = parts.Count();
if (count > partsNumber)
{
parts[0] = parts[0].Concat(parts[count - 1]);
parts.RemoveAt(count - 1);
}
return parts;
}
public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> items,
int partitionSize)
{
int i = 0;
return items.GroupBy(x => i++ / partitionSize).ToArray();
}
}
If you are working in .net I can just recommend to use Framework 4. There you can use a lot of powerfull utilities all around paralelling. I like the new Task class a lot, before I was using always the default threads. Just a tip: if you have somewhere a slow foreach-loop or something else, give a look to the power of parallel-foraech (currently I'm using it a lot of times):
http://msdn.microsoft.com/de-de/library/system.threading.tasks.parallel.foreach.aspx
You will find a lot of other helpful functions for the Tasks :)
Sorry for the late response. You are right with your remark. But anyway I had a good reason to use this functionality :) I had this problem: I had a generic of this type List that contains tausends of entries. I worked through this enumerable in a foreach loop, but this was too slow for me. So I wanted to parallelize the loop.
First I converted the list to the threadsave concurrentQueue, and I started 4 tasks, where each-one took one file after each other. The next approach was to run 4 Tasks (additionally I love the Tasks and the TaskFactory from .Net 4 framework) , where each task got a part of the enumerable to work through. This was the much faster aproach in my case (but it depends on the problem you have). Anyway I was using the code above :)