【问题标题】:Dynamically generated double quoted string in lambda expression C#在 lambda 表达式 C# 中动态生成的双引号字符串
【发布时间】:2016-05-31 20:30:02
【问题描述】:

我有数字,我用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\",.... 而我的代码没有返回任何内容

感谢您的帮助!谢谢

【问题讨论】:

  • 它应该是"RespId": { "5","6","7","8",... 请看图片
  • 为什么要将字符串数组转换为包含大量转义引号的字符串?只需传递未转义字符串的数组即可。
  • 以上是用于ElasticSearch和Elasticsearch的lambda表达式查询,当我们发送没有双引号字符串时会抛出异常

标签: c# c#-4.0 elasticsearch lambda


【解决方案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)                                    
                                )))));

我希望它对你有用。

【讨论】:

  • 再次感谢@maxCoder 让我实现它并尽快回复你:)
  • 嘿,它对代码和简短描述非常有用:)
  • 谢谢,很高兴你知道了,别忘了把它标记为答案
猜你喜欢
  • 2015-01-22
  • 2016-08-20
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 2021-11-14
  • 2017-06-04
  • 1970-01-01
  • 2018-01-25
相关资源
最近更新 更多