.NET是否有办法来检查,如果一个表中包含列表B中的所有项目? [英] Does .NET have a way to check if List a contains all items in List b?

查看:151
本文介绍了.NET是否有办法来检查,如果一个表中包含列表B中的所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法:

namespace ListHelper
{
    public class ListHelper<T>
    {
        public static bool ContainsAllItems(List<T> a, List<T> b)
        {
            return b.TrueForAll(delegate(T t)
            {
                return a.Contains(t);
            });
        }
    }
}

其中的目的在于,以确定是否一个列表中包含的另一个列表中的所有元素。这样看来,我认为这样的事情会被内置到.NET已经是该案件与我在复制功能?

The purpose of which is to determine if a List contains all the elements of another list. It would appear to me that something like this would be built into .NET already, is that the case and am I duplicating functionality?

编辑:我的道歉不说明了前面,我使用单声道版本2.4.2本code

My apologies for not stating up front that I'm using this code on Mono version 2.4.2.

推荐答案

如果您使用的是.NET 3.5,它很容易:

If you're using .NET 3.5, it's easy:

public static bool ContainsAllItems(List<T> a, List<T> b)
{
    return !b.Except(a).Any();
}

这将检查是否有 B ,这是不 A 的所有元素 - 然后反转结果

This checks whether there are any elements in b which aren't in a - and then inverts the result.

这篇关于.NET是否有办法来检查,如果一个表中包含列表B中的所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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