首页 > 解决方案 > 使用 Microsoft.Azure.Cosmos 时如何获取已编译的查询

问题描述

使用 nugetMicrosoft.Azure.DocumentDB及其DocumentClient类时,您可以获得将针对 cosmos 执行的已编译查询。像这样:

var documentClient = new DocumentClient(new Uri(endpoint), authKey);

var query = documentClient
    .CreateDocumentQuery<Product>(collectionUrl, queryOptions)
    .Where(x => x.Id == "apple-iphone")
    .Select(x => new
    {
        x.Id
    })
    .AsDocumentQuery();

// Here you can inspect the compiled query: "SELECT VALUE {"Id": root["id"]} FROM root WHERE (root["id"] = "apple-iphone")" 
var rawQuery = query.ToString();

但是,在使用时Microsoft.Azure.Cosmos,我看不到任何已编译的查询或在调试和检查变量时如何提取它:

var container = new CosmosClientBuilder(endpoint, authKey)
    .WithConnectionModeDirect()
    .WithThrottlingRetryOptions(TimeSpan.FromSeconds(30), 10)
    .Build()
    .GetContainer(databaseId, collectionId);

var query = container
    .GetItemLinqQueryable<Product>(requestOptions: queryOptions)
    .Where(x => x.Id == "apple-iphone")
    .Select(x => new
    {
        x.Id
    })
    .ToFeedIterator();

// How do I get the compiled query here???
var rawQuery = query.ToString();

无论如何,有没有得到编译查询将在使用时针对 cosmos 执行Microsoft.Azure.Cosmos

标签: c#azureazure-cosmosdb

解决方案


在你打电话之前,ToFeedIterator()你可以先打电话queryable.ToQueryDefinition().QueryTextIQueryable也可以ToString()直接调用获取 SQL 查询定义,就像在 v2 中一样。


推荐阅读