C # - I know I can do this in LINQ, but I cannot get it to work

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

I need to output the distribution of the number of Regions merged for each number. For example given:

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

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

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

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

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

I need to get something like:

number: 1 - total 10 participants

number: 3 - total 6 members

number: 4 - total 9 members

I've been doing various tutorials and it seems to me that I need to use groups, but I couldn't get anything to work.

Any help? Thank!

0


source to share


2 answers


Answering this similar question might help:

Linq query with SUM and ORDER BY



or this

C # list <gt; GroupBy 2 values

+1


source


You need to group by 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)};

      

0


source







All Articles