【问题标题】:Sum in Linq to Object query vb.net在 Linq 中求和到对象查询 vb.net
【发布时间】:2013-11-27 16:41:40
【问题描述】:

我有一个包含这些信息的数据表:

|"Repere"|"Position"|"Prix"|  
|"AA"    | "1"      | 1800 |  
|"AA"    | "1"      | 5200 |  
|"AA"    | "2"      | 1500 |  
|"AA"    | "2"      | 0300 |  
|"BB"    | "1"      | 0010 |  

“Repere”和“Position”列是字符串。
列“Prix”为 Double。

我想在按“Repere”和“Position”分组后对“Prix”列中的值求和。
我改进了使用 linq 对象查询,但我不知道我必须如何做才能得出总和。

我给你我提出的问题:

    Dim ReqPrixModel = From Row In dTable_Devis.Rows _
                       Group Row.item("Prix") By Rep = Row.Item("Repere"), Pos = Row.item("Position") Into Group _
                       Select New With {.Prix = From i In dTable_Devis.Rows
                                                Where (i.Item("Repere") = Rep And i.item("Position") = Pos)
                                                Select (i.Item("Prix"))}  

【问题讨论】:

    标签: vb.net linq datatable linq-to-objects


    【解决方案1】:

    这里有一个代码可以满足你的需求:

    Dim ReqPrixModel = From Row In dTable_Devis _
                  Group DirectCast(Row, DataRow).Item("Prix") By Rep = Row.Item("Repere"), Pos = Row.Item("Position") Into grouped = Group _
                  Select New With {.Prix = grouped.Sum(Function(r) DirectCast(r, Double))}
    

    【讨论】:

    • 非常感谢 Varocarbas。它完全符合我的预期。
    猜你喜欢
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 2011-10-15
    • 2011-03-06
    相关资源
    最近更新 更多