2017-04-02 11 views
0

다음 코드는 Microsoft.Azure.DocumentDB.Core (버전 1.2.1) 패키지와 LINQ를 사용하여 DocumentDb 데이터베이스에 대한 쿼리를 작성하는 ASP.NET 코어 API입니다.DocumentDb .NET Core SDK - IQueryable.OrderBy가 잘못된 쿼리를 생성합니다.

그러나 생성 된 검색어가 잘못되었습니다.

public async Task<IEnumerable<T>> QueryAsync 
    (Expression<Func<T, bool>> predicate, string orderByProperty) 
{ 
    client.CreateDocumentQuery<T>(myCollectionUri) 
     .Where(predicate) 
     .OrderBy(x => orderByProperty) 
     .AsDocumentQuery(); 

    /* rest of the code... */ 
} 

// And I call it: 
await repository.QueryAsync<Book>(x => x.Author == "Joe", "Price"); 

// class Book is a POCO with 3 properties: string Title, string Author, and decimal Price 

/* 
INVALID generated query: 
    SELECT * FROM root WHERE (root["Author"] = "Joe") ORDER BY "Price" 

Correct query: 
    SELECT * FROM root WHERE (root["Author"] = "Joe") ORDER BY root["Price"] 
*/ 

이 동작을 변경할 수 있습니까?

아마 IQueryable<T>에 확장 메서드를 작성하고이처럼 호출하여 :

client.CreateDocumentQuery<T>(myCollectionUri) 
    .Where(predicate) 
    .DocumentDbOrderBy(orderByProperty) // new extension method 
    .AsDocumentQuery(); 
+0

'OrderBy' 여전히'AuthorId'을, 당신이 들어 않습니다 왜 당신이 그것을 원하는 '가격'과 함께있는 것. 또한 생성 된 쿼리에 대해 걱정하는 대신 'Price Asc'로 잘못된 결과를 얻고 있음을 제안 할 수 있습니까? –

+0

@MrinalKamboj 실제 시나리오를 기반으로 샘플을 만들었습니다. 방금 질문 수정 – Poulad

답변

0

나는 동적 LINQ를 사용하여이 문제를 해결.

추가하여 내 저장소 클래스에 문 :

using System.Linq.Dynamic.Core; 

그리고 방법에있어서 작은 변화 :

public async Task<IEnumerable<T>> QueryAsync 
    (Expression<Func<T, bool>> predicate, string orderByProperty) 
{ 
    client.CreateDocumentQuery<T>(myCollectionUri) 
     .Where(predicate) 
     .OrderBy(orderByProperty) // uses dynamic LINQ 
     .AsDocumentQuery(); 

    /* rest of the code... */ 
}