c# - 在 CosmosClient 中使用 LINQ 查询时查找 RequestCharge
问题描述
当我使用CreateItemAsync
然后我得到一个ItemResponse
它允许我访问RequestCharge
RU 并可能记录它,这样我就可以准确地了解我对 RU 的使用。
但是,当使用 LINQ 使用 CosmosClient 进行查询时,我看不到任何获取 RequestCharge 的方法。看到 RequestCharge 非常有用,但它似乎只有在我以某种方式查询时才可用,所以我认为我一定遗漏了一些东西。
这是我的代码示例。
var tenantContainer = cosmos.GetContainer("myapp", "tenant");
var query = tenantContainer.GetItemLinqQueryable<Tenant>(true, null,
new QueryRequestOptions { PartitionKey = new PartitionKey("all") })
.Where(r => r.AccountId = "1234");
var tenants = query.ToList();
//track.Metric("GetTenants", cosmosResponse.RequestCharge);
请注意,我使用的是“新”CosmosClient
而不是旧的DocumentClient
.
解决方案
确保包含此 using 语句。
using Microsoft.Azure.Cosmos.Linq;
然后你可以使用.ToFeedIterator()
which contains a property with RequestCharge
。
这是完整的代码示例:
var container = _cosmos.GetContainer("mydb", "user");
// Normal linq query
var query = container.GetItemLinqQueryable<Shared.Models.User>(true, null,
new QueryRequestOptions { PartitionKey = new PartitionKey(tenantName) })
.Where(r => r.Email == loginRequest.Email);
// Instead of getting the result, first convert to feed iterator
var iterator = query.ToFeedIterator();
// And finally execute with this command that also supports paging
var cosmosResponse = await iterator.ReadNextAsync();
// And then the RequestCharge is readily available
_track.Metric("GetUserForAuthentication", cosmosResponse.RequestCharge);
// And whatever linq execution you wanted to do, you can do on the response
var user = cosmosResponse.FirstOrDefault();
推荐阅读
- git - git cherry-pick 抛出“樱桃挑选或恢复已经在进行中”
- php - 我正在学习 PHP,但我的包含不起作用
- kotlin - Kotlin:如何使用变量指定命名参数?
- excel - 如果 F 列中的单元格包含文本“待定”,如何突出显示从 A 到 F 的整行
- javascript - d3 circle pack - 访问数据(用于文本标签)
- android - 如何在wearOS android中默认添加并发症
- qt - Qt 组合模式。取决于背景的黑色或白色
- python - 如何创建具有自动增量和条件 pandas 的列
- javascript - 使用 getFieldDecorator 正确设置 Antd DatePicker 初始值
- python-3.x - 如何在列python 3.7中应用组内特定行的值