在lambda表达式C#中动态生成双引号字符串

时间:2016-02-19 09:59:38

标签: c# c#-4.0 elasticsearch lambda

我使用concatenating double quoted动态添加到列表中的数字,并按如下方式输入代码

ArrayList arrRespId = new ArrayList();
for (int i = 0; i < hitsCountRespId; i++)
{
  arrRespId.Add("\"" + jsonRespId["aggregations"]["my_fields"]["buckets"][i]["key"].ToString() + "\"");
} //Adding all numbers in the list with double quoted 

var strDouble = string.Join(",", arrRespId.ToArray()); //"5","6","7"

嘿伙计我有lambda expression的代码,可以从Elasticsearch

获取信息
var SearchAggregate = client.Search<string>(sd => sd
                    .Index("Index")
                    .Type("Table")
                    .Size(0)
                    .Query(q => q
                        .Bool(b => b
                            .Must(
                                m => m.Terms("QID", new[] { strDouble.tostring() }),
                                m => m.Terms("ProjectID", new[] { "50" }),
                                m => m.Terms("RespID", new[] { abc.ToString() })
                                //m => m.Terms("RespID", new[] { "1","2" })
                                )))));

但问题是当我将这个双引号字符串放在lambda表达式中时它采用"\"5\",\"6\",\"7\",....并且我的代码没有返回任何内容

See what my lambda expression takes it as a request

我会感谢您的帮助!谢谢

1 个答案:

答案 0 :(得分:1)

我看到你的代码和图像输出错误地理解了term在lambda表达式中的期望,它不期望双引号或字符
term期望包含所需数字的字符串数组,代码应如下所示

string[]  arrRespId= new arrRespId[50]; //in your scenario you can put `hitsCountRespId` as array size
for (int i = 0; i < hitsCountRespId; i++)
{
  arrRespId[i]= jsonRespId["aggregations"]["my_fields"]["buckets"][i]["key"].ToString();
} 

然后将string array放在lambda表达式中,如下所示

var SearchAggregate = client.Search<string>(sd => sd
                    .Index("Index")
                    .Type("Table")
                    .Size(0)
                    .Query(q => q
                        .Bool(b => b
                            .Must(
                                m => m.Terms("QID", new[] { strDouble.tostring() }),
                                m => m.Terms("ProjectID", new[] { "50" }),
                                m => m.Terms("RespID", arrRespId)                                    
                                )))));

我希望它对你有用。