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: 
You can use `Skip` and `Take` in a simple `for` to accomplish what you want

```
   var groupSize = (int)Math.Ceiling(query.Count() / 3d);
   var result = new List<List<Item>>();
   for (var j = 0; j < 3; j++)
      result.Add(query.Skip(j * groupSize).Take(groupSize).ToList());

```
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]`