2011-05-24 105 views
1

我有一個字符串像這樣的列表:獲取唯一值

{"100", "101, "101", "102, "103, "103", "104", "104", "105"}

我需要得到一個新的字符串列表,只有不同的值:

{"100","101","102","103","104","105"}

任何人都有一個快速的方法來做到這一點?

+0

可能DUPLI這個問題的解答: http://stackoverflow.com/questions/292307/selecting-unique-elements-from-a-list-in-c – yas4891 2011-05-24 17:03:51

回答

3
List<String> strings = new List<string>() { "100", "101", "101", "102", "103", "103", "104", "104", "105" }; 
var distinctStrings = strings.Distinct().ToList(); 
5

您可以使用DISTINCT方法:

List<string> distinctList = dupeList.Distinct().ToList();

0
List<string> dupes = new List<string>(){"100", "101, "101", "102, "103, "103", "104", "104", "105"}; 
List<string> no_dupes = dupes.Distinct().ToList(); 

或者你可以使用一個HashSet

var noDupes = new HashSet<string>(dupes).ToList(); 

另見Remove Duplicates from a List in C#