Last post Oct 08, 2018 09:44 AM by Yuki Tao
Oct 07, 2018 01:46 PM|rameezshaikh56|LINK
I have records of parent elements which also have child elements. Parent as well as child has ExtendedListPrice field. I need to show sum of this field whether the data is of Parent or child. How to achieve this? Shall I first get the total of all Parent
elements and then add sum of all child elements?
Oct 07, 2018 03:59 PM|DA924|LINK
How to achieve this? Shall I first get the total of all Parent elements and then add sum of all child elements?
That's how you do it. 1 parent value + add sum of all children using the Sum() aggregate.
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: