繁体   English   中英

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

[英]Get unique values from a List of strings

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

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

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

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

任何人都有一个快速的方法来做到这一点?

您可以使用 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();

或者你可以使用HashSet

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

另请参阅C# 中的从列表中删除重复项

暂无
暂无

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

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