如何创建LINQ
查询以实现以下目标:
Groups
按 Value 属性排序,最大的组位于顶部SumProduct
计算值 x 成熟度的Group
,并将结果设置为 MaturitySumProduct 属性EM> MyNewClass 下面是来自LINQ
查询的源数据和所需结果结构的示例。
我开始在LINQ查询中进行分组,但却被其他人忽略了:
Dim query = From i In SourceDataItems
Group By Name = i.Name
Into MyNewClass = Group
如何完成查询以获得所需的结果?
答案 0 :(得分:2)
在MyNewClass
中设定适当的构造函数:
Dim query = From i In SourceDataItems
Group By Name = i.Name Into NameGroup = Group
Let SumArea = NameGroup.Sum(Function(i) i.Area)
Let SumValue = NameGroup.Sum(Function(i) i.Value)
Let SumMaturity = NameGroup.Sum(Function(i) i.Maturity)
Let MaturitySumProduct = NameGroup.Sum(Function(i) i.Value * i.Maturity)
Order By SumValue Descending
Select New MyNewClass(Name, SumArea, SumValue, MaturitySumProduct)