【发布时间】:2017-08-27 02:20:46
【问题描述】:
以下代码适用于 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();
【问题讨论】:
-
为什么您的
OrderBy仍然包含AuthorId,而您希望它与Price一起使用。另外,您可以建议您使用Price Asc得到不正确的结果,而不是担心生成的查询 -
@MrinalKamboj 我根据我的真实场景制作了示例。刚刚编辑了问题
标签: c# linq .net-core iqueryable azure-cosmosdb