在vb.net中的linq sum

时间:2010-02-18 16:10:42

标签: vb.net linq

我需要创建一个类似于SQL的查询:

SELECT  CodProiect, SUM(Valoare)
FROM DET
WHERE CodProiect = 'cod_pr'
GROUP BY CodProiect;

如何在LINQ中编写相同的内容?

我试过这个:

dim gac1 = (From ac1 In t_detalii _
            Where ac1!CodProiect = cod_pr1 _
            Select ac1!Valoare).Sum(Function(ac1) ac1!Valoare)

但是它给了我一个错误“找不到类型'Double'的默认成员。”

1 个答案:

答案 0 :(得分:2)

你需要选择ac1而不是ac1!Valoare(属性本身,而不是相应的对象)

dim gac1 = (From ac1 In t_detalii _
                       Where ac1!CodProiect = cod_pr1 _
                       Select ac1).Sum(Function(ac1) ac1!Valoare)

修改

您当然可以在查询中对其进行分组:

dim gac1 = (From ac1 In t_detalii _
                       Where ac1!CodProiect = cod_pr1 _
                       Group By ac1.CodProiect Into ac2
                       Select ac2.Sum(Function(ac2) ac2!Valoare)

(那个方向的东西,我不熟悉vb.net语法,我也是在没有compilersupport atm的情况下写的。