Take the 2-minute tour ×

Is there a way to create a linq to sql query to group by a Take parameter?

For instance if I have a Table that has 20 records with one unique ID value from 1 to 20, i would like to get a group of records grouped by 5 records:

Group 1: 1,2,3,4,5
Group 2: 6,7,8,9,10

I can think of two ways to do this

By making 5 queries: The first query to count the total records, and the next 4 queries would be select queries where i skip 5 and take 5.

And by making one query, looping trough the results with an inner index and creating objects with the groups of 5

Is there a more elegant way to do this with linq to sql?

share|improve this question
i'd add a "groupId" field to the table and use that one in a group by clause –  Alex Nov 16 '11 at 15:13
The orderBy may not be always the same, so in different situations items can have different groups –  Atzoya Nov 16 '11 at 15:20
Short answer is that linq doesn't have anything built in, apart from skip/take. Is this for a paged table? Just wondering why you need to get all the groups in one go. I generally get a count first and then do skip/takes when the user flips through the pages. –  James McCormack Nov 16 '11 at 15:22
I have menu in a site where there are too many menu items in one level that causes the design to go wayward, so as a solution i plan to group the items from one level into groups of 10 or something to save up on vertical space. I know i can go with one of the solutions, but I wanted to know if there is a shorter and more elegant way of doing this (I remember Rails having a very elegant soluton to this same issue) –  Atzoya Nov 16 '11 at 15:29
in that case i would just get the lot and loop through with a counter, using mod to inject your group breaks. –  James McCormack Nov 16 '11 at 15:34

4 Answers 4

up vote 1 down vote accepted

Your second idea is exactly what I would do. Just get everything from the database and loop on the .NET side. Probably there are ways to use Aggregate to do it in a more LINQ-esque way but I am sure they will be harder to read. If you do it in a lazy fashion (use yield to implement enumerator) you will still loop through the sequence only once so you will not lose performance.

share|improve this answer

If you're going to end up retrieving all the records from the database anyways, why not just go ahead and do it then use something like this:

collection.GroupBy(x => collection.IndexOf(x) / 5);

share|improve this answer
I made an example using the most simple data I could think of, the actual table doesn't have values from 1 to 20 –  Atzoya Nov 16 '11 at 15:17
collection.GroupBy(x => collection.IndexOf(x) / 5); –  twaggs Nov 16 '11 at 15:24
Elegant it is, but i think it will ruin performance. –  Atzoya Nov 16 '11 at 15:35

Can you group like this

var items = from i in arr
                let m = i / 5
                group i by m into d
                select new { d };

If you had 10 elements it will create two groups of 5 each

share|improve this answer

Pull your data as-is, feed it to the container then split em up.

Your queries should never, ever be aware of anything concerning how the data they pull is shown to the user.

share|improve this answer

Your Answer


