Solved

sum property of observable collection

Posted on 2014-01-10
7
927 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

0
Comment
Question by:solarissf
  • 4
  • 3
7 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39771341
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

0
 

Author Comment

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

Author Comment

by:solarissf
ID: 39771349
something like this..
but does not work

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

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39771354
Please see my post and how I implemented the code.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39771368
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.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39771400
This should also work if you wish to go that route.

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

Open in new window

0
 

Author Comment

by:solarissf
ID: 39771406
yepp... I just figured it out and it was the same as your last post... thanks!!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now