0

我有一个“A”类列表,有两个属性 qty 和 groupId.eg

      数量组 ID
       8 1
       2 2
       2 3
       1 4
       3 5

我需要根据一个因子值对它进行分组。例如让因子值=4。基于这个因子值组看起来像:

     数量组 ID
      4 1 - 组 1
      4 1 - 组 2
      2 2 -|
      2 3 -| 组3
      1 4 -|  
      3 5 -| 第 4 组   

我需要一个 linq 表达式。在此先感谢。

4

1 回答 1

0

通常,linq 中的分组是通过调用方法 GroupBy() 来完成的,其中 lambda 表达式说明如何分组。

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.groupby.aspx

// your list
IList<ClassA> listToGroup = new List<ClassA>(){ ... your items ...};
// Factor
int factor = 4;
// group the list by Qty value of the ClassA-items
var resultOfGrouping = listToGroup.GroupBy(a => a.Qty * factor == 8);

Hth 托比

于 2012-06-27T07:16:00.257 回答