# Group each 10 items into a group in LINQ

• ### Question

• Hi guys,

How can I group each 10 items in a group using LINQ ?

Monday, February 18, 2013 2:48 PM

### Answers

• If i understand your question, you want to group every 10 items in a collection irrespective of any values they contain at individual elements. I dont think LINQ [or any collection framework for that matter] offer an out of the box solution for this, this is because you are not necessarily providing a criteria for grouping related to the entities in the collection.

If your collection contains a class or structure, consider adding a counter field to it. Then write code to iterate through the collection and set the counter to one up value for every 10 items. Then use this field to write your grouping logic with LINQ.

However, these are just ideas without any concrete foundation. We may be able to help better if you can explain what you are trying to do.

• Proposed as answer by Thursday, February 21, 2013 7:52 PM
• Marked as answer by Saturday, February 23, 2013 2:40 PM
Monday, February 18, 2013 3:32 PM
• If you want to split into groups, then try this generic recursive implementation as well. (It can be made as an extension function and called from your LINQ expressions).

static IEnumerable<IEnumerable<T>> GetGroups<T>(IEnumerable<T> e, int groupSize)

{

if (!e.Any()) return new IEnumerable<T>[0];

var g = new[] { e.Take(groupSize) };

var r = e.Skip(groupSize);

return g.Concat(GetGroups(r, groupSize));

}

• Edited by Monday, February 18, 2013 8:44 PM
• Proposed as answer by Monday, February 18, 2013 10:44 PM
• Unproposed as answer by Monday, February 18, 2013 10:45 PM
• Proposed as answer by Thursday, February 21, 2013 7:53 PM
• Marked as answer by Saturday, February 23, 2013 2:40 PM
Monday, February 18, 2013 8:37 PM

### All replies

• Hi,

for group by in LINQ you can go through this article : http://www.codeproject.com/Articles/35667/How-to-Use-LINQ-GroupBy

One good question is equivalent to ten best answers.

Monday, February 18, 2013 3:23 PM
• If i understand your question, you want to group every 10 items in a collection irrespective of any values they contain at individual elements. I dont think LINQ [or any collection framework for that matter] offer an out of the box solution for this, this is because you are not necessarily providing a criteria for grouping related to the entities in the collection.

If your collection contains a class or structure, consider adding a counter field to it. Then write code to iterate through the collection and set the counter to one up value for every 10 items. Then use this field to write your grouping logic with LINQ.

However, these are just ideas without any concrete foundation. We may be able to help better if you can explain what you are trying to do.

• Proposed as answer by Thursday, February 21, 2013 7:52 PM
• Marked as answer by Saturday, February 23, 2013 2:40 PM
Monday, February 18, 2013 3:32 PM
• Something like this should work:

myenumeration.Select((item, index) => new { item, index }).GroupBy(o => o.index / 10, o => o.item)

Monday, February 18, 2013 3:46 PM
• If you want to split into groups, then try this generic recursive implementation as well. (It can be made as an extension function and called from your LINQ expressions).

static IEnumerable<IEnumerable<T>> GetGroups<T>(IEnumerable<T> e, int groupSize)

{

if (!e.Any()) return new IEnumerable<T>[0];

var g = new[] { e.Take(groupSize) };

var r = e.Skip(groupSize);

return g.Concat(GetGroups(r, groupSize));

}

• Edited by Monday, February 18, 2013 8:44 PM
• Proposed as answer by Monday, February 18, 2013 10:44 PM
• Unproposed as answer by Monday, February 18, 2013 10:45 PM
• Proposed as answer by Thursday, February 21, 2013 7:53 PM
• Marked as answer by Saturday, February 23, 2013 2:40 PM
Monday, February 18, 2013 8:37 PM