chuckd
chuckd

Reputation: 14520

How would I distinct my list of key/value pairs

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

Answers (2)

MarcinJuraszek
MarcinJuraszek

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

Robadob
Robadob

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

Related Questions