简体   繁体   English

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

[英]Get unique values from a List of strings

I have a List of strings like this:我有一个这样的字符串列表:

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

And I need get a new list of strings with only the different values:我需要得到一个新的字符串列表,其中只有不同的值:

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

Anyone have a quick way to do this?任何人都有一个快速的方法来做到这一点?

You can use the Distinct method:您可以使用 Distinct 方法:

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

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

Or you could use a HashSet或者你可以使用HashSet

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

Also see Remove Duplicates from a List in C#另请参阅C# 中的从列表中删除重复项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM