[英]How to get different elements from a List<List<string>>?
我有一個包含幾個字符串列表的列表。
我正在尋找一個LINQ查詢,該查詢可以返回一個列表,其中包含所有子列表中存在的所有不同字符串。
只需一個查詢就可以嗎?
謝謝。
List<string> a = {"a", "b", "c"}
List<string> b = {"c", "d", "e"}
List<List<string>> c = {a, b}
對'c'進行查詢后的預期結果:
List<string> result = {"a", "b", "c", "d", "e"}
您可以先使用SelectMany
來平坦化List<List<string>>
,然后再使用Distinct
:
var input = new List<List<string>> {a,b};
var result = input.SelectMany(x=>x).Distinct().ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.