We help IT Professionals succeed at work.

sum property of observable collection

solarissf
solarissf asked
on
1,524 Views
Last Modified: 2016-02-10
Hi guys,

I am trying to sum a property in my observable collection.  

Tried below using LINQ and not working, any idea what I'm doing wrong?

                var summationResult =
                        from p in BreakdownClass
                        select into g
                        {
                            previousPos = g.Sum(p => p.tPrevPos),
                            previousPL = g.Sum(p => p.tPrevPL),
                            topPos = g.Sum(p => p.tTopTrd),
                            topPL = g.Sum(p => p.tTopPL),
                            netPos = g.Sum(p => p.tNetPos),
                            netPL = g.Sum(p => p.tNetPL)
                        };

Open in new window

Comment
Watch Question

Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi solarissf;

Give it a try like this.

var summationResult =
    from p in BreakdownClass 
    group p by "OneGroup" into g
    select new
    {
        previousPos = g.Sum(p => p.tPrevPos),
        previousPL = g.Sum(p => p.tPrevPL),
        topPos = g.Sum(p => p.tTopTrd),
        topPL = g.Sum(p => p.tTopPL),
        netPos = g.Sum(p => p.tNetPos),
        netPL = g.Sum(p => p.tNetPL)
    };

Open in new window

Author

Commented:
that's the catch... I don't want to group by anything... I just want to sum up some properties without grouping

Author

Commented:
something like this..
but does not work

                var summationResult =
                    
                    BreakdownClass.Sum(x => x.tPrevPL),
                    BreakdownClass.Sum(x => x.tPrevPos),
                    ;

Open in new window

Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Please see my post and how I implemented the code.
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Each iteration of the query returns one record so either you group in the query or you iterate through all returned records and sum them up.
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
yepp... I just figured it out and it was the same as your last post... thanks!!!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.