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?
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
None
0 Points
1 Post
How to get sum of all parent and child elements in linq?
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?
Contributor
4973 Points
4264 Posts
Re: How to get sum of all parent and child elements in linq?
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.
Contributor
3710 Points
1431 Posts
Re: How to get sum of all parent and child elements in linq?
Oct 08, 2018 09:44 AM|Yuki Tao|LINK
Hi rameezshaikh56,
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:
Note: the SUM in SQL over 0 rows is NULL, not 0
or as lambdas (from comments):
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.