2017-04-02 52 views
0

以下代碼適用於ASP.NET Core使用Microsoft.Azure.DocumentDB.Core(版本1.2.1)包和LINQ構建針對DocumentDb數據庫的查詢的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語句到我的倉庫類:

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... */ 
}