C# 检查集合中给定项的出现情况
我想检查集合中某些给定项目的出现情况,并认为它是这样工作的,但事实并非如此C# 检查集合中给定项的出现情况,c#,.net,C#,.net,我想检查集合中某些给定项目的出现情况,并认为它是这样工作的,但事实并非如此 public static bool ContainsAny<T>(this IEnumerable<T> collection, IEnumerable<T> otherCollection) { if (otherCollection == null) throw new ArgumentNullException("otherCollect
public static bool ContainsAny<T>(this IEnumerable<T> collection, IEnumerable<T> otherCollection)
{
if (otherCollection == null)
throw new ArgumentNullException("otherCollection", ExceptionResources.NullParameterUsed);
if (collection == null)
throw new ArgumentNullException("collection", ExceptionResources.ExtensionUsedOnNull);
else
return Enumerable.Any<T>(otherCollection, new Func<T, bool>(((Enumerable)collection).Contains<T>));
}
publicstaticboolcontainsany(此IEnumerable集合,IEnumerable其他集合)
{
if(otherCollection==null)
抛出新ArgumentNullException(“otherCollection”,ExceptionResources.NullParameterUsed);
if(集合==null)
抛出新ArgumentNullException(“collection”,ExceptionResources.ExtensionUsedOnNull);
其他的
返回Enumerable.Any(其他集合,新Func(((Enumerable)集合).Contains));
}
如果指定的集合包含任何其他集合项,我希望得到true,否则为false。
但有一个错误告诉我无法在system.linq.Enumberable中转换system.collections.generic.iEnumerable>T>。我的错误在哪里?这应该可以:
return otherCollection.Any(collection.Contains);
调用
Contains
方法不需要任何强制转换,因为已有一个将IEnumerable
作为第一个参数的方法。当任一集合为空(非空)时,您应该考虑所需的答案。不像看上去那么琐碎。