2017-09-27 4 views
1

을 수행, 나는 다음과 같은 코드가 다음 CurrentTestResultController.cs에서중 하나로, OData-V4 엔티티의 컬렉션 동작 후 내 중 하나로, OData-V4 컨트롤러에서 기능

var fn = reportModelBuilder.EntityType<CurrentTestResult>() 
     .Collection.Function("Breakdown").Returns<int>(); 

을, 나는 믿을 수 없 간단한 있습니다

[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)] 
[HttpGet] 
public IHttpActionResult Breakdown() 
    { 
    var count = dataService.GetAll() 
       .Select(x => x.TestResultTypeId) 
       .Distinct() 
       .Count(); 

    return Ok(count); 
    } 

기본적으로 컬렉션에있는 모든 CurrentTestResult 엔티티의 경우 집합에서 고유 한 TestResultTypeId을 반환합니다. (이것은 사소한 작업입니다,하지만 훨씬 더 복잡 정말 삶 시나리오 간체) 쉽게 할 수 있었다

을 -하지만 난 처음 필터에없는 것이 켜져 작동해야 CurrentTestResult의 수집 . 기본적으로 모든 CurrentTestResult 엔티티에

로컬 호스트/응용 프로그램/odatareport/CurrentTestResult/Default.Breakdown

반환

{ 
@odata.context: "http://localhost/app/odatareport/$metadata#Edm.Int32", 
value: 5 
} 

(A 올바른 결과를 운영

이 요청, 별개의 5 거기 유형)

그러나이 요청은 필터를 먼저 내림차순으로 시도하면 실패합니다.

이것이 실패 이유를 로컬 호스트/응용 프로그램/odatareport/CurrentTestResult/Default.Breakdown? $ 상단

= 2 개

반환 지금까지 내가 하나로, OData 파이프 라인을 이해

{ 
error: { 
code: "", 
message: "The query specified in the URI is not valid. The requested resource is not a collection. Query options $filter, $orderby, $count, $skip, and $top can be applied only on collections.", 
innererror: { 
message: "The requested resource is not a collection. Query options $filter, $orderby, $count, $skip, and $top can be applied only on collections.", 
type: "Microsoft.OData.ODataException", 
stacktrace: 
" at System.Web.OData.EnableQueryAttribute.ValidateSelectExpandOnly(ODataQueryOptions queryOptions) at System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor, ODataQueryContext queryContext) at System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)" 
    } 
    } 
} 

는 의미가 있습니다. 컨트롤러 메서드는 IQueryable을 반환하고 ODATA $ 필터, $ top 등이 적용됩니다.

나는 이미 필터링 된 집합을 조작하는 기능을 원합니다.

내가 할 수있는 일이 가능한 것입니까?

내가 고장 방법 자체가 그 안에 .GetAll()를 가지고 있지만, MUT 방법 전에 필터링을 적용 할 수있는 방법이있을 수 있음을 얻을 - 그렇지 않으면

,이 모든 아주 무의미 ....

답변

3

두 가지 옵션 :

1) 중 하나로, OData는 $count 엔드 포인트 (this 볼을 가지고 있지만 $count의 두 가지 형태가있다 - 엔드 포인트 api/collection/$count 및 시스템 쿼리 옵션 api/collection?$count=true이, 당신은 엔드 포인트를 원하는 것)의 수를 반환 컬렉션 (EnableQuery으로 필터링 할 수 있음). 함수를 다른 GET 콜렉션 메소드로 취급하고 계산하려는 (이 경우에는 distinct : TestResultTypeId) 쿼리를 리턴 한 다음 클라이언트가 $count 엔드 포인트를 요청하면됩니다.)

public IHttpActionResult Get(ODataQueryOptions<CurrentTestResult> queryOptions) 
{ 
    // base query with ODataQueryOptions applied 
    var query = queryOptions.ApplyTo(dataServcie.GetAll()) 
     as IQueryable<CurrentTestResult>; 

    // get distinct TestResultTypeId's and then count 
    var count = query.Select(x => x.TestResultTypeId) 
      .Distinct() 
      .Count(); 

    return Ok(count); 
} 

을 내가 지금 그렇게하지 못할 테스트 모바일 해요하지만이 충분히 가까이 (그렇지 않으면 정확해야 :

2) 당신의 행동 방식에 대한 ODataQueryOptions<T> 매개 변수를 정의하고 수동 옵션을 적용 당신이 가야 할 곳으로 데려다 줄 것입니다. 문제가있는 경우 알려 주시면 답변을 업데이트하겠습니다.

+0

매우 가까이에 있습니다. - 제가 말씀 드렸듯이, 코드를 단계별로 실행하면 옵션이 적용됩니다.하지만 클라이언트에서는 https : // imgur를 얻습니다. com/a/dU32b – Matt

+0

Nevermind, AllQueryOptions 특성을 삭제하는 것을 잊었습니다 - 다시 한 번 감사드립니다! – Matt

+0

훌륭함, 문제 없음 – Moho

관련 문제