首页 文章

LINQ查询到Group,Sort和SumProduct

提问于
浏览
0

如何创建 LINQ 查询以实现以下目标:

  • 按名称属性对源数据项进行分组

  • 按值属性对结果 Groups 进行排序,最大的组在顶部

  • 将组项目选择到名为MyNewClass的新类中(如下图所示,位于右侧)

  • 为每个 Group 计算值x成熟度的 SumProduct ,并将结果设置为MyNewClass中的MaturitySumProduct属性 .

下面是来自 LINQ 查询的源数据和所需结果结构的示例 .

enter image description here

我开始在LINQ查询中进行分组,但却被其他人所困扰:

Dim query = From i In SourceDataItems
            Group By Name = i.Name
            Into MyNewClass = Group

如何完成查询以获得所需的结果?

1 回答

  • 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)
    

相关问题