且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#linq中的多个列表中查找常见项目

更新时间:2023-02-17 19:55:58

var data = new [] {
    new List<int> {1, 2, 3, 4, 5},
    new List<int> {6, 7, 8, 9, 1},
    new List<int> {3, 6, 9, 2, 0, 1},
    new List<int> {1, 2, 9, 0, 5},
    new List<int> {1, 7, 8, 6, 5, 4},
    new List<int> {1},
    new List<int> {},
    null
};

IEnumerable<int> temp = null;
foreach (var arr in data)
    if (arr != null && arr.Count != 0)
        temp = temp == null ? arr : arr.Intersect(temp);