在C#linq中的多个列表中查找常见项目 [英] Find common items in multiple lists in C# linq

查看:33
本文介绍了在C#linq中的多个列表中查找常见项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了搜索,但仅找到与两个列表相关的答案.但是当他们超过两个时呢?

I searched, but I found only answers which related to two lists. But what about when they are more than two?

List 1 = 1,2,3,4,5
List 2 = 6,7,8,9,1
List 3 = 3,6,9,2,0,1
List 4 = 1,2,9,0,5
List 5 = 1,7,8,6,5,4
List 6 = 1
List 7 =

如何获得常见物品?如您所见,其中一个是空的,所以公用将是空的,但是我需要跳过空列表.

How to get the common items? as you can see one of them is empty, so the common will be empty, but I need to skip empty lists.

推荐答案

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);

这篇关于在C#linq中的多个列表中查找常见项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆