簡體   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