User1520731567 posted
Hi rameezshaikh56,
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.
For example:
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:
https://stackoverflow.com/questions/5893445/sum-nested-values-with-linq
Best Regards.
Yuki Tao