我有List<List<int>>以下值:
[1] = 1,2
[2] = 4,5,6
[3] = 1,3
Run Code Online (Sandbox Code Playgroud)
我想要做的是检索具有唯一值=> 1,2,3,4,5,6(在原始列表中重复的第一个)的简单List.
如果是1D List,我会用
variable.Select(n=>n).Distinct();
Run Code Online (Sandbox Code Playgroud)
但是当我试图使用时
variable.Select(n=>n.Select(x=>x)).Distinct();
Run Code Online (Sandbox Code Playgroud)
我得到了2D列表(我猜有独特的值).我怎么解决这个问题?谢谢
你可以只使用SelectMany与Distinct
var uniqueList = list.SelectMany(x => x).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
SelectManyflattents List<List<int>>成a IEnumerable<int>并Distinct消除重复.