Solved

sum property of observable collection

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

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 63

Expert Comment

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

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 63

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hey!!!! 1 22
How to parse the string and split the sub directory names in asp.net 7 43
VB.NET 2008 - SQL Timeout 9 25
Problem with SelectList in .NET MVC application 1 18
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!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

832 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