I would like to know how to group item and split every N record by using LINQ
I have this table contains 22 ITEM01 and each quantity is 10, 2 ITEM02 with quantity 50 for each
# |ITEM |QUANTITY
==================
1 |ITEM01| 10
2 |ITEM01| 10
3 |ITEM01| 10
. . .
. . .
22|ITEM01| 10
23|ITEM02| 50
24|ITEM02| 50
How to get the expected result below by using LINQ? (Group by item if count >10, if not, let it shows one by one.
ITEM |QUANTITY
=================
ITEM01 | 100
ITEM01 | 100
ITEM01 | 10
ITEM01 | 10
ITEM01 | 10
ITEM02 | 50
ITEM02 | 50
I've tried following code, but it cannot achieve
var query =
items.GroupBy(item => item.Name)
.SelectMany(g => g.Select((item, index) => new { item, index })
.GroupBy(x => x.index / 10)
.Select(batch => new Item {
Name = batch.First().item.Name,
Quantity = batch.Sum(x => x.item.Quantity)
})).OrderBy(item => item.Name);
Thanks for your help