Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个包含几个字符串列表的列表。
我正在寻找一个 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:
SelectMany
List<List<string>>
Distinct
var input = new List<List<string>> {a,b}; var result = input.SelectMany(x=>x).Distinct().ToList();