Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sum property of observable collection

Posted on 2014-01-10
7
Medium Priority
?
1,043 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 64

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 64

Expert Comment

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

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

782 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