以下代码适用于 ASP.NET Core API,该API使用Microsoft.Azure.DocumentDB.Core
(版本1.2.1)程序包和LINQ构建针对DocumentDb数据库的查询。
但是,生成的查询无效!
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 :(得分: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... */
}