查找类型列表中是否包含类型列表 [英] Find whether a List of type is contained in a List of Lists of type

查看:73
本文介绍了查找类型列表中是否包含类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种LINQ方式来确定是否,例如:

Is there a LINQ way to find out whether, for example:

List<int> micro = new List<int>(){1,2,3,4,5};

在一个

List<List<int>> macro = new List<List<int>>();

?

我可以对宏执行foreach循环,并检查它是否至少包含一个包含所有micro项(macro[0].Contains(1) && macro[0].Contains(2)...)List,但是还有其他C#方式吗?

I can do a foreach loop on macro and check whether it contains at least one List that has all of micro's items (macro[0].Contains(1) && macro[0].Contains(2)...), but is there a more C# way to do it?

推荐答案

bool exists = macro.Any(x => micro.All(x.Contains));

如果元素的顺序和数量很重要,请使用SequenceEqual

If the order and the number of element is important use SequenceEqual

bool exists = macro.Any(x => x.SequenceEqual(micro));

这篇关于查找类型列表中是否包含类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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