Reputation: 14520
If I have a list List<KeyValuePair<string,string>>
ex.
["abc","123"]
["asc","123"]
["asdgf","123"]
["abc","123"]
how can I distinc this list?
Upvotes: 12
Views: 15997
Reputation: 125620
Distinct by both Key
and Value
:
var results = source.Distinct().ToList();
Distinct by Key
or Value
(just change the property on GroupBy
call:
var results = source.GroupBy(x => x.Key).Select(g => g.First()).ToList();
Upvotes: 27
Reputation: 5349
You should use a Set (of pair objects) if you wish to have distinct pairs or a Map/Dictionary if you wish to have distinct keys.
Upvotes: -1