Question: I have a query which I get as:

```
var query = Data.Items
            .Where(x => criteria.IsMatch(x))
            .ToList<Item>();

```

This works fine.

However now I want to break up this list into x number of lists, for example 3. Each list will therefore contain 1/3 the amount of elements from query.

Can it be done using LINQ?

Answer: 
If the order of the elements doesn't matter using an `IGrouping` as suggested by Daniel Imms is probably the most elegant way (add `.Select(gr => gr.Select(e => e.item))` to get an `IEnumerable<IEnumerable<T>>`).

If however you want to preserve the order you need to know the total number of elements. Otherwise you wouldn't know when to start the next group. You can do this with LINQ but it requires two enumerations: one for counting and another for returning the data (as suggested by Esteban Elverdin).

If enumerating the query is expensive you can avoid the second enumeration by turning the query into a list and then use the [`GetRange`](http://msdn.microsoft.com/en-us/library/21k0e39c.aspx) method:

```
public static IEnumerable<List<T>> SplitList<T>(List<T> list, int numberOfRanges)
{
    int sizeOfRanges = list.Count / numberOfRanges;
    int remainder = list.Count % numberOfRanges;

    int startIndex = 0;

    for (int i = 0; i < numberOfRanges; i++)
    {
        int size = sizeOfRanges + (remainder > 0 ? 1 : 0);
        yield return list.GetRange(startIndex, size);

        if (remainder > 0)
        {
            remainder--;
        }

        startIndex += size;
    }
}

static void Main()
{
    List<int> list = Enumerable.Range(0, 10).ToList();

    IEnumerable<List<int>> result = SplitList(list, 3);

    foreach (List<int> values in result)
    {
        string s = string.Join(", ", values);
        Console.WriteLine("{{ {0} }}", s);
    }
}

```

The output is:

```
{ 0, 1, 2, 3 }
{ 4, 5, 6 }
{ 7, 8, 9 }

```
You can create an extension method:

```
public static IList<List<T>> GetChunks<T>(this IList<T> items, int numOfChunks)
{
    if (items.Count < numOfChunks)
        throw new ArgumentException("The number of elements is lower than the number of chunks");
    int div = items.Count / numOfChunks;
    int rem = items.Count % numOfChunks;

    var listOfLists = new List<T>[numOfChunks];

    for (int i = 0; i < numOfChunks; i++)
        listOfLists[i] = new List<T>();

    int currentGrp = 0;
    int currRemainder = rem;
    foreach (var el in items)
    {
        int currentElementsInGrp = listOfLists[currentGrp].Count;
        if (currentElementsInGrp == div && currRemainder > 0)
        {
            currRemainder--;
        }
        else if (currentElementsInGrp >= div)
        {
            currentGrp++;
        }
        listOfLists[currentGrp].Add(el);
    }
    return listOfLists;
}

```

then use it like this :

```
var chunks = query.GetChunks(3);

```

N.B.

in case of number of elements not divisible by the number of groups, the first groups will be bigger. e.g. `[0,1,2,3,4] --> [0,1] - [2,3] - [4]`