tags:

views:

84

answers:

2

I got a list of RegionGroup objects with two properties: an int "number" and a list<> of "Regions".

I need to output a distribution of number of Region objects combined for each number. For example, given:

[Region 1] - number: 1 - list<> has 5 members

[Region 2] - number: 3 - list<> has 2 members

[Region 3] - number: 4 - list<> has 9 members

[Region 4] - number: 1 - list<> has 5 members

[Region 5] - number: 3 - list<> has 4 members

I need to get something like:

number:1 - total 10 members

number:3 - total 6 members

number:4 - total 9 members

I've been messing around with various tutorials, and I think I need to use groups, but I couldn't quite get anything working.

Any help? Thanks!

+1  A: 

The answer to this similar question may help:

http://stackoverflow.com/questions/820552/linq-query-with-sum-and-order-by

or this one

http://stackoverflow.com/questions/363655/c-list-groupby-2-values

Brian
looked at the first one and arrived at a working solution. thanks for the quick reply!
DarkAmgine
I hate 0 rep accepted answers, so here's a +1 from me
Brian Rasmussen
A: 

You need to group by the region number and then get the total number of members in the lists.

How about something like this (untested):

var regionCounts = from region in RegionGroups
                   group region by region.number into g
                   select new {number = g.Key, 
                               regions = g.Sum(r=>r.Regions.Count)};
lc
yup, this is what i got =]thanks for the confirmation!
DarkAmgine