Solved

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

Posted on 2012-03-26
3
2,421 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 75

Expert Comment

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

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

Open in new window

0
 
LVL 75

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

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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