从动态类型变量中检索值

时间:2017-04-08 10:26:13

标签: c# azure-cosmosdb

我在c#中调用documentdb select count,就像这样

$ python -m line_profiler profiling.py.lprof
Timer unit: 1e-06 s

查询的结果是动态类型变量count,其值为{2}

从变量中检索值2的最佳方法是什么?

3 个答案:

答案 0 :(得分:1)

试试这个

var indx1 = result.IndexOf("{");
var indx2 = result.IndexOf("}");
str = str.Substring(indx1+1, indx2-1);

您还可以使用正则表达式来实现此目标

Jquery get substring between parenthesis

请不要忘记提及您的结果。

答案 1 :(得分:0)

调用count.ToString()实际上给了我“2”然后我将它解析为int。

答案 2 :(得分:0)

根据我的理解,您可以在检索结果时指定在调用IDocumentQuery.ExecuteNextAsync时返回的对象的类型,而不显式地进行类型转换。您可以按如下方式修改代码:

var result = await this.documentClient.CreateDocumentQuery<int>("{documentCollectionUri}","select value count(1) from c").AsDocumentQuery().ExecuteNextAsync<int>();
var count = result.FirstOrDefault(); // 2,int
相关问题