从字符串列表中获取唯一值

时间:2011-05-24 17:02:04

标签: c# list

我有一个像这样的字符串列表:

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

我需要获得一个只有不同值的新字符串列表:

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

任何人都有快速的方法吗?

3 个答案:

答案 0 :(得分:5)

您可以使用Distinct方法:

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

答案 1 :(得分:3)

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

答案 2 :(得分: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#