I originally have a dictionary of <string, List<ProviderSummary>>
called rowsDictionary
Now for each key of that dictionary I group its list of values by some criteria as below:
Dictionary<string, List<ProviderSummary>> providerGroups = rowsDictionary.ToDictionary(
x => x.Key,
v => v.Value.GroupBy(x => new { x.GroupID, x.GroupFin, x.ZipCode })
.Select(x => x.First())
.ToList());
so for example if key["1234"]
originally had 6 items in its list of values, now it may have two items based on that grouping. My question and confusion is what happens to the rest of the values? ( those four) and what values will go in to these two lists that are returned for the group?
Group by works by taking whatever you are grouping and putting it into a collection of items that match the key you specify in your group by clause.
If you have the following data:
Member name Group code
Betty 123
Mildred 123
Charli 456
Mattilda 456
And the following query
var query = from m in members
group m by m.GroupCode into membersByGroupCode
select membersByGroupCode;
The group by will return the following results:
![](https://www.manongdao.com/static/images/pcload.jpg)
You wouldn’t typically want to just select the grouping directly. What if we just want the group code and the member names without all of the other superfluous data?
We just need to perform a select to get the data that we are after:
var query = from m in members
group m by m.GroupCode into membersByGroupCode
let memberNames = from m2 in membersByGroupCode
select m2.Name
select new
{
GroupCode = membersByGroupCode.Key,
MemberNames = memberNames
};
Which returns the following results:
![](https://www.manongdao.com/static/images/pcload.jpg)
What values will go in to the lists that are returned for the group?
The first for each group, because you do:
.Select(x => x.First())
What happens to the rest of the values?
They will not be projected into your target dictionary.
Your LINQ group by query takes the original list, performs additional grouping on it, and then prunes the list based on that grouping.
Consider a situation where a single list contains these items:
GroupID GroupFin ZipCode Name
------- -------- ------- ----
1 1 94111 A
1 1 94111 B
1 1 94111 C
1 1 94111 D
1 2 94110 E
1 2 94110 F
Group by would make two groups out of this list of six:
GroupID=1 GroupFin=1 ZipCode=94111
GroupID=1 GroupFin=2 ZipCode=94110
The first group would contain providers A, B, C, and D; the second group would contain E and F.
The next thing that your query does is applying First
. This operation picks the initial item from the group; in this case, it would be A and E. The remaining items are thrown ignored.