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

x
?
Solved

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

Posted on 2012-03-26
3
Medium Priority
?
2,673 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

773 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