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?
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.