2014-09-10 1 views
0

에 대한 .ANY()를 사용할 수 없습니다 I 컨트롤러에서 다음 코드, 내가 response.Customers.Any를 (사용하고자하는 목록일반적인 목록

을 반환 서비스 동작을)를 호출하고, 같이가 난 특정 고객을 찾고있을 것입니다. 하지만 가끔은. 아무 것도 없습니다. .Any()를 사용할 때 컴파일 오류가 발생합니다.

내가 작업을 호출하는 방식에 따라 다르면 확실하지 않습니까? 난 항상 우리가 일반 목록 .ANY()를 사용할 수 있다고 생각하기 때문에

컨트롤러

public class Customer 
{ 
    public Customer(ICustomerService customerService) 
    { 
     this.CustomerService = customerService; 
    } 

    private ICustomerService CustomerService { get; set; } 

    var response = this.ExecuteServiceCall(() => this.CustomerService.getCustomerResults()); 

    response.customers.Any(x => x.group == "A"); 

} 

답변

1

AnyIEnumerable<T> 인터페이스를위한 확장 방법이지만, 그것의 구현은 System.Linq 네임 스페이스에 정적 Enumerable 클래스에있는 . 그것은 다음과 같은 : 당신은 확장 메서드로 사용하려는 경우

using System; 
namespace System.Linq 
{ 
    public static class Enumerable 
    { 
     ... 

     public static bool Any<TSource>(
      this IEnumerable<TSource> source, 
      Func<TSource, bool> predicate) 
     { 
      foreach (var item in source) 
      { 
       if (predicate(item)) 
       { 
        return true; 
       } 
      } 
      return false; 
     } 

     ... 
    } 
} 

당신은 using System.Linq; 문을 추가해야합니다.

또는 이해를 위해, 당신은 당신이 당신의 수업을 System.Linq을 포함했는지가 .ANY()를 사용할 수 있도록합니다

bool b = System.Linq.Enumerable.Any(response.customers, x => x.group == "A"); 
0

로 부를 수

using System.Linq; 
0

먼저 System.Linq 네임 스페이스를 포함시켜야합니다.

그런 다음 response.customers

  • 구현합니다 IEnumerable<T>, 당신은 갈 수 있어야한다.

  • 가 명시 적으로 당신이 원하는 인터페이스로 캐스팅해야합니다, IEnumerable<T> 구현 :

  • bool hit = ((IEnumerable<Customer>)(response.customers)) 
          .Any(x => x.group == "A") 
          ; 
    
    IEnumerable<T>를 구현하지 않습니다,하지만 제네릭이 아닌 IEnumerable를 구현, 당신은해야합니다 다르게 캐스트 :

    bool hit = response.customers 
          .Cast<Customer>() 
          .Any(x => x.group == "A") 
          ; 
    
0

는 고객의 목록입니다 객체의 속성이 반환되었거나 객체 자체입니까? 이 작품이 좋아질까요?

var customers = this.ExecuteServiceCall(() => this.CustomerService.getCustomerResults()); 

if (customers.Any(c => c.group == "A") 
{ 
    . . . 
} 
관련 문제