DocumentDB .Net 客户端库 LINQ 聚合错误

DocumentDB .Net Client Library LINQ Aggregates Error

我刚刚开始使用 DocumentDB,想尝试一下最近添加的聚合功能。但是,当我尝试使用它们时,出现以下异常: Query expression is invalid, expression return type System.Int32 is unsupported. Query must evaluate to IEnumerable.

我已经有一个 DocumentClient 对象,在下面命名为 client

var collectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
var options = new FeedOptions {MaxItemCount = -1, EnableCrossPartitionQuery = true};

var count = client.CreateDocumentQuery<LogEvent>(collectionUri, options)
    .Where(f => f.Player == "SomePlayer")
    .Count();

我正在使用本地模拟器。该集合中有四个文档。

这是目前在本地模拟器中不可用的东西吗?还是我做错了什么?

根据您的代码,我针对我的 Azure DocumentDB(而不是本地模拟器)对其进行了测试,然后我可能会遇到与您提供的相同的错误:

正如 Aravind Ramachandran 提到的版本,然后我检查了我安装的版本,发现它是 Microsoft.Azure.DocumentDB 1.11.4。将版本更新到1.12.1或更高版本后,我可以检索到预期的结果。

这里是release notes Azure DocumentDB for .NET 如下:

Changes in 1.12.1

Added LINQ support for aggregation queries (COUNT, MIN, MAX, SUM, and AVG).