Solved

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

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

932 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

12 Experts available now in Live!

Get 1:1 Help Now