Link to home
Start Free TrialLog in
Avatar of Stephen Forero
Stephen ForeroFlag for United States of America

asked on

sum property of observable collection

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

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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

Avatar of Stephen Forero

ASKER

that's the catch... I don't want to group by anything... I just want to sum up some properties without grouping
something like this..
but does not work

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

Open in new window

Please see my post and how I implemented the code.
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.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yepp... I just figured it out and it was the same as your last post... thanks!!!