C# .NET 是否有办法检查列表 a 是否包含列表 b 中的所有项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1520642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does .NET have a way to check if List a contains all items in List b?
提问by Matt Haley
I have the following method:
我有以下方法:
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);
});
}
}
}
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?
其目的是确定一个列表是否包含另一个列表的所有元素。在我看来,像这样的东西已经内置到 .NET 中了,是这样吗,我是否在复制功能?
Edit: My apologies for not stating up front that I'm using this code on Mono version 2.4.2.
编辑:我很抱歉没有预先说明我在 Mono 2.4.2 版上使用此代码。
采纳答案by Jon Skeet
If you're using .NET 3.5, it's easy:
如果您使用 .NET 3.5,这很容易:
public class ListHelper<T>
{
public static bool ContainsAllItems(List<T> a, List<T> b)
{
return !b.Except(a).Any();
}
}
This checks whether there are any elements in b
which aren't in a
- and then inverts the result.
这会检查是否有任何b
不在a
- 中的元素,然后反转结果。
Note that it would be slightly more conventional to make the methodgeneric rather than the class, and there's no reason to require List<T>
instead of IEnumerable<T>
- so this would probably be preferred:
请注意,使方法通用而不是类会稍微更传统,并且没有理由要求List<T>
代替IEnumerable<T>
- 所以这可能是首选:
public static class LinqExtras // Or whatever
{
public static bool ContainsAllItems<T>(this IEnumerable<T> a, IEnumerable<T> b)
{
return !b.Except(a).Any();
}
}
回答by drzaus
Just for fun, @JonSkeet's answeras an extension method:
只是为了好玩,@JonSkeet 的回答是一种扩展方法:
/// <summary>
/// Does a list contain all values of another list?
/// </summary>
/// <remarks>Needs .NET 3.5 or greater. Source: https://stackoverflow.com/a/1520664/1037948 </remarks>
/// <typeparam name="T">list value type</typeparam>
/// <param name="containingList">the larger list we're checking in</param>
/// <param name="lookupList">the list to look for in the containing list</param>
/// <returns>true if it has everything</returns>
public static bool ContainsAll<T>(this IEnumerable<T> containingList, IEnumerable<T> lookupList) {
return ! lookupList.Except(containingList).Any();
}
回答by user3210251
You could also use another way. Override equals and use this
你也可以用另一种方式。覆盖等于并使用它
public bool ContainsAll(List<T> a,List<T> check)
{
list l = new List<T>(check);
foreach(T _t in a)
{
if(check.Contains(t))
{
check.Remove(t);
if(check.Count == 0)
{
return true;
}
}
return false;
}
}
回答by Thomas
Included in .NET 4: Enumerable.All
包含在 .NET 4 中: Enumerable.All
public static bool ContainsAll<T>(IEnumerable<T> source, IEnumerable<T> values)
{
return values.All(value => source.Contains(value));
}