2014-04-06 9 views
0

은 ASP.NET 웹 API 중 하나로, OData 만들 사용 가능을가 포함 유사한중 하나로, OData 오류

List<string> customersTitles = Odata.OrdersService.Select(o=>o.CustomerTitle).Distinct().ToList(); 

List<Customer> customers = Odata.CustomerService.Where(m => customersTitles .Contains(m.CustomerTitle)) 

가져 오기 오류 :

Error translating Linq expression to URI: The expression value(System.Collections.Generic.List`1[System.String]).Contains([10007].CustomerTitle) is not supported.}

API :

public class CustomerController : EntitySetController<Customer, int> 
{ 

    [Queryable] 
    public override IQueryable<Customer> Get() 
    { 
     Expression filter = this.QueryOptions.Filter.ToExpression<Customer>(); 
     return db.Query<Customer>(filter as Expression<Func<Customer, bool>>); 
    } 
} 

답변

5

클라이언트 측에 존재하는 문자열 목록이 서버 측 자원이 아니기 때문에 Contains 구문은 URI를 통해 지원되지 않습니다.

Linq2Sql 공급자에는 Contains에 대한 선천적 인 번역이 있으며, 이는 SQL의 IN 절로 변환됩니다.

OData에서는 이러한 변환이 지원되지 않습니다. 당신이 작성해야하는 것은에 대한 확장 된 쿼리 목록에 모든 제목 값을 사용하여 절 여기서

이 작동하지 않기 때문에 :

List<Customer> customers = Odata.CustomerService.Where(m => customersTitles .Contains(m.CustomerTitle)) 

확장 된 쿼리 옵션과 같은 쿼리를 작성에 도움이 :

를 여기
List<Customer> customers = Odata.CustomerService.Where(m => m.CustomerTitle == customerTitles[0] || m.CustomerTitle == customerTitles[1]); // and so on 

필터 구축하기위한 코드이다

var titleFilterList = customerTitles.Select(title => String.Format("(CustomerTitle eq {0})", title)); 
var titleFilter = String.Join(" or ", titleFilterList); 
var customers = Odata.CustomerService.AddQueryOption("$filter", titleFilter).Execute().ToList(); // you may have to cast this. 

T 멋진 확장 메소드를 사용하고 동적 표현식 기반 술어를 작성하여 강력하게 형식화 된 방식으로 동일한 작업을 수행하는 또 다른 옵션이 있습니다. 다음 단계를 따르십시오.

http://blogs.msdn.com/b/phaniraj/archive/2008/07/17/set-based-operations-in-ado-net-data-services.aspx

관련 문제