Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I distinct my list of key/value pairs

Tags:

c#

list

linq

If I have a list List<KeyValuePair<string,string>>ex.

["abc","123"]
["asc","123"]
["asdgf","123"]
["abc","123"]

how can I distinc this list?

like image 446
user1186050 Avatar asked Jul 30 '13 19:07

user1186050


People also ask

How do I get distinct items from a list?

List<int> myList = list. Distinct(). ToList();

How do you define key-value pairs?

A key-value pair consists of two related data elements: A key, which is a constant that defines the data set (e.g., gender, color, price), and a value, which is a variable that belongs to the set (e.g., male/female, green, 100). Fully formed, a key-value pair could look like these: gender = male. color = green.

What are the kind of key-value pairs?

Answer: A key-value pair (KVP) is a set of two linked data items: a key, which is a unique identifier for some item of data, and the value, which is either the data that is identified or a pointer to the location of that data. Key-value pairs are frequently used in lookup tables, hash tables and configuration files.


1 Answers

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();
like image 124
MarcinJuraszek Avatar answered Oct 07 '22 02:10

MarcinJuraszek