Reputation: 235
I am having following linq -
var quantity = (from p in context.StoreInventory
where p.BookId== BookId
&& p.StoreAddress == StoreAddress
select p).Sum(i => i.Quantity);
I am getting error -
The method 'Sum' is not supported
Can anyone tell me the reason and required changes.
Upvotes: 4
Views: 1603
Reputation: 148180
Use Enumerable.ToList before you call Sum to convert the query to collection.
var quantity = (from p in context.StoreInventory
where p.BookId== BookId
&& p.StoreAddress == StoreAddress
select p).ToList().Sum(i => i.Quantity);
Edit: This will bring all the row and will apply the sum which is not efficient way of doing. As you need to sum up quantity you can select quanity instead of row.
var quantity = (from p in context.StoreInventory
where p.BookId== BookId
&& p.StoreAddress == StoreAddress
select p.Quantity).Sum();
Upvotes: 3
Reputation: 125660
var quantity = (from p in context.StoreInventory
where p.BookId== BookId
&& p.StoreAddress == StoreAddress
select p.Quantity).Sum();
This should work - the sum is performed on 'Quality'
column, which is taken using select
statement. That's because Sum(expression)
is not supported by LINQ to Entities, but standard Sum()
is.
Whole work should be done by database, so no rows will be retrieved by application - just single number.
Upvotes: 11