c#LINQ Distinct()在多级列表上

Rob*_* J. 1 c# linq

我有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列表(我猜有独特的值).我怎么解决这个问题?谢谢

Sel*_*enç 8

你可以只使用SelectManyDistinct

var uniqueList = list.SelectMany(x => x).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)

SelectManyflattents List<List<int>>成a IEnumerable<int>Distinct消除重复.