점검

2014-04-26 2 views
1

내가 컬렉션의 일부 특정 항목의 발생을 확인하고자하고 같이 일을 생각하지만점검

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>)); 
} 

내가 싶어하지 않습니다 지정된 컬렉션에 otherCollection 항목이 포함되어 있으면 true이고 그렇지 않으면 false입니다. 하지만 system.collections.generic.iEnumerable> T>를 system.linq.Enumberable로 변환 할 수 없다는 오류가 있습니다. 내 실수는 어디 갔지?

+0

컬렉션이 비어 있거나 null이 아닐 때 원하는 대답을 생각해야합니다. 외모만큼 사소한 것은 아닙니다. –

답변

3

이 작동합니다 : 이미 첫 번째 매개 변수로 IEnumerable<T>을 취하는 extension method이 있기 때문에

return otherCollection.Any(collection.Contains); 

당신은 Contains 메소드를 호출하는 모든 캐스트가 필요하지 않습니다.

+0

위대한 작품 : D 조 감사합니다. – patdhlk