Query CosmosDB with CosmosClient and map to object
现在我有以下代码来查询我的 CosmosDB:
1 2 3 4 5 6 7 8 9 10 11 12 | var streamIterator = containerLogs.GetItemQueryStreamIterator("SELECT * FROM mycontainer"); while (streamIterator.HasMoreResults) { var results = await streamIterator.ReadNextAsync(); var stream = results.Content; using var reader = new StreamReader(stream); string data = await reader.ReadToEndAsync(); // typeof what..? //var dbDocuments = JsonConvert.DeserializeObject<???>(data); } |
变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | { "_rid":"wDhMAJ9xYHE=", "Documents": [{ "id":"c05c4eee-32d4-458a-8ae8-d22dd0f93839", "CustomProperty1":"Value 1", "CustomProperty2":"Value 2", "_rid":"wDhMAJ9xYHECAAAAAAAAAA==", "_self":"dbs\\/wDhMAA==\\/colls\\/wDhMAJ9xYHE=\\/docs\\/wDhMAJ9xYHECAAAAAAAAAA==\\/", "_etag":""00000000-0000-0000-7167-28c107aa01d6"", "_attachments":"attachments\\/", "_ts": 1597319093 }], "_count": 1 } |
模型是这样的
1 2 3 4 5 6 7 | public class MyModel { [JsonProperty(PropertyName ="id")] public Guid Id {get;set;} public string CustomProperty1 {get;set;} public string CustomProperty1 {get;set;} } |
那么如何查询我的 CosmosDB 以便它返回此类的列表?
这里有一些我觉得很有帮助的示例。更简单的情况是使用类型化的