How to sum field in already exist Linq result [modified]
-
I need to sum field depending on 2 tables with 1 to many relations
customer
Id name
1 a
2 b
3 cStock
CustID qty
1 10
1 15
2 20I have somewhere in my project the following
Dim lqcus = From c In db0.customers Where c.id = txt_idcust.Text Select c
I managed to sum by 2 ways
Dim sum1 = (From s In db0.stocks Where s.CustID = txt_idcust.Text Select c.qty).Sum
Dim sum2 = Aggregate s In db0.stocks Where s.CustID = txt_idcust.Text Into sum(s.qty)But the above 2 will be executed again in the sql server to get the result, but I already have the lqcus which is linq result for the customer and inside the lqcus there is the stock already filtered by the 1 to many relation For example, I can write the following
Dim sum3 = From s In lqcus.stocks Where s.CustID = txt_idcust.Text
But I could not find the syntax to sum the qty Can someone help?
modified on Tuesday, July 19, 2011 6:36 AM
-
I need to sum field depending on 2 tables with 1 to many relations
customer
Id name
1 a
2 b
3 cStock
CustID qty
1 10
1 15
2 20I have somewhere in my project the following
Dim lqcus = From c In db0.customers Where c.id = txt_idcust.Text Select c
I managed to sum by 2 ways
Dim sum1 = (From s In db0.stocks Where s.CustID = txt_idcust.Text Select c.qty).Sum
Dim sum2 = Aggregate s In db0.stocks Where s.CustID = txt_idcust.Text Into sum(s.qty)But the above 2 will be executed again in the sql server to get the result, but I already have the lqcus which is linq result for the customer and inside the lqcus there is the stock already filtered by the 1 to many relation For example, I can write the following
Dim sum3 = From s In lqcus.stocks Where s.CustID = txt_idcust.Text
But I could not find the syntax to sum the qty Can someone help?
modified on Tuesday, July 19, 2011 6:36 AM
I manage to solve it.
Dim sum3 = Aggregate s In lqcus.stocks Into Sum(s.qty)
Thanks to every one who look at it.