Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ-带条件的分组列表_C#_Linq_Group By - Fatal编程技术网

C# LINQ-带条件的分组列表

C# LINQ-带条件的分组列表,c#,linq,group-by,C#,Linq,Group By,读过了吗 但我想进一步了解 假设一个列表包含22个ITEM01,每个ITEM01的数量为10,以及2个ITEM02,每个ITEM02的数量为50 # |ITEM |QUANTITY ================== 1 |ITEM01| 10 2 |ITEM01| 10 3 |ITEM01| 10 . . . . . . 22|ITEM01| 10 23|ITEM02| 50 24|ITEM02| 50 如何获得如下结果:(如果计数>10,转到下一行) 谢

读过了吗

但我想进一步了解

假设一个列表包含22个ITEM01,每个ITEM01的数量为10,以及2个ITEM02,每个ITEM02的数量为50

# |ITEM  |QUANTITY
==================
1 |ITEM01| 10
2 |ITEM01| 10
3 |ITEM01| 10
.     .     .
.     .     .
22|ITEM01| 10
23|ITEM02| 50
24|ITEM02| 50
如何获得如下结果:(如果计数>10,转到下一行)


谢谢你的帮助

检查代码中的注释以查看发生了什么以及查询的实际功能

// input generation
var input = Enumerable.Range(1, 22)
                      .Select(x => new { ID = x, Item = "ITEM01", Quantity = 10 })
                      .Concat(Enumerable.Range(23, 2)
                                        .Select(x => new { ID = x, Item = "ITEM02", Quantity = 50 }));

// query you're asking for
var output =
    input.GroupBy(x => x.Item) // group by Item first
         .Select(g => g.Select((v, i) => new { v, i }) // select indexes within group
                       .GroupBy(x => x.i / 10)  // group items from group by index / 10
                       .Select(g2 => g2.Select(x => x.v))  // skip the indexes as we don't need them anymore
                       .SelectMany(g2 =>  // flatten second grouping results and apply Sum logic
                           g2.Count() == 10  
                               // if there are 10 items in group return only one item with Quantity sum
                           ? new[] { new { Item = g.Key, Quantity = g2.Sum(x => x.Quantity) } }
                               // if less than 10 items in group return the items as they are
                           : g2.Select(x => new { Item = g.Key, Quantity = x.Quantity })))
          .SelectMany(g => g);  // flatten all results

你为什么需要这个?
// input generation
var input = Enumerable.Range(1, 22)
                      .Select(x => new { ID = x, Item = "ITEM01", Quantity = 10 })
                      .Concat(Enumerable.Range(23, 2)
                                        .Select(x => new { ID = x, Item = "ITEM02", Quantity = 50 }));

// query you're asking for
var output =
    input.GroupBy(x => x.Item) // group by Item first
         .Select(g => g.Select((v, i) => new { v, i }) // select indexes within group
                       .GroupBy(x => x.i / 10)  // group items from group by index / 10
                       .Select(g2 => g2.Select(x => x.v))  // skip the indexes as we don't need them anymore
                       .SelectMany(g2 =>  // flatten second grouping results and apply Sum logic
                           g2.Count() == 10  
                               // if there are 10 items in group return only one item with Quantity sum
                           ? new[] { new { Item = g.Key, Quantity = g2.Sum(x => x.Quantity) } }
                               // if less than 10 items in group return the items as they are
                           : g2.Select(x => new { Item = g.Key, Quantity = x.Quantity })))
          .SelectMany(g => g);  // flatten all results