.NET是否有办法来检查,如果一个表中包含列表B中的所有项目?办法、项目、列表、NET

2023-09-02 01:25:00 作者:难得一生

我有以下方法:

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.