tags:

views:

139

answers:

1
+1  Q: 

linq sum in vb.net

I need to make a query that look like this is SQL:

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

How can I write the same thing in LINQ?

I have attempted this:

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

But it gives me an error "No default member found for type 'Double'."

+2  A: 

You need to select ac1 instead of ac1!Valoare (which is the property itself, not the corresponding object)

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

edit

You can of course group it in the query already:

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)

(something in that direction, I'm not familiar with the vb.net syntax, also I'm writing it without compilersupport atm.

Femaref
thanks a lot, i was starring at it for a long time :)
Iulian