I have the following item set from an XML:
id category
5 1
5 3
5 4
5 3
5 3
I need a distinct list of these items:
5 1
5 3
5 4
How can I distinct for Category AND Id too in LINQ?
Are you trying to be distinct by more than one field? If so, just use an anonymous type and the Distinct operator and it should be okay:
If you’re trying to get a distinct set of values of a “larger” type, but only looking at some subset of properties for the distinctness aspect, you probably want
DistinctByas implemented in MoreLINQ inDistinctBy.cs:(If you pass in
nullas the comparer, it will use the default comparer for the key type.)