Solved

C# get sum of dictionary values when values are stored as a collection

Posted on 2012-03-26
3
2,376 Views
Last Modified: 2012-06-21
Hello Experts!

I have a Dictionary storing data as such: Dictionary<string, IList<T>>();

I am trying to select the total number of values without having to use nested foreach loops.

I found this using LinQ,

myRowCnt = deptToEmp.Skip(1).Sum(x => x.Value);

Open in new window


But because value is a Collection, this will not work. I was trying to modify it myself, but I am not familiar with LinQ.

Any help would be greatly appreciated!
0
Comment
Question by:I_s
  • 2
3 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 37768614
Try:

double total = deptToEmp.Values.Sum(x => x.Sum());

Open in new window

0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37768629
Or you might modify your original:

myRowCnt = deptToEmp.Skip(1).Sum(x => x.Value.Sum());

Open in new window

0
 

Author Comment

by:I_s
ID: 37768722
Hey thanks for the responses, I'll try them both when I get back into the office.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

11 Experts available now in Live!

Get 1:1 Help Now