Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ error - The method 'Sum' is not supported

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.

like image 339
RTRokzzz Avatar asked Dec 09 '22 18:12

RTRokzzz


1 Answers

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.

like image 187
MarcinJuraszek Avatar answered Dec 11 '22 07:12

MarcinJuraszek