Oct 08, 2018 09:44 AM|Yuki Tao|LINK
hall I first get the total of all Parent elements and then add sum of all child elements?
I think so,I suggest you could use SelectMany() and
Sum() to get all child elements' sum firstly, then get all parent elements' sum by the same way.
var total = (from user in db.authors
from books in user.book
select (int?)books.Id).Sum() ?? 0;
Note: the SUM in SQL over 0 rows is NULL, not 0
or as lambdas (from comments):
var total = db.authors.SelectMany(user => user.book).Sum(books=> (int?)books.Id) ?? 0;
I find the similar question from this link,you could refer to it: