Solved

sum property of observable collection

Posted on 2014-01-10
7
976 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

756 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