I just found this lambda expression:
myCustomerList.GroupBy(cust => cust.CustomerId).Select(grp => grp.First());
Correct me if I am wrong, but with this lambda you can distinct the myCustomerList on the CustomerId and that’s exaclty what I need. But I am trying to figure out how it works.
The first step is the groupby: this result in a dictionary, IGouping<long, Customer> with the CustomerId as the key of the dictionary.
Second a select takes place and this is the part I don’t get. The select selects a customer, but how can it select a Customer from a dictionary? You need a key for this, because of the group by. Where’s that key? And how is First() helping here?
Can you tell me in detail how the last part works?
It’s not selecting it from the dictionary – it’s saying for each grouping in the result of
GroupBy, select the first entry. Note thatIGrouping<TKey, TElement>implementsIEnumerable<TElement>.Basically a group has two things:
This is selecting the first element from each group.