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

标签 c# c#-4.0 elasticsearch lambda

我在列表中添加了数字,动态连接双引号并按如下方式放入代码中

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 表达式 的代码,它从 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

我会感谢你的帮助!谢谢

最佳答案

我看到了您的代码和图像输出,您在理解 lambda 表达式中 term 所期望的内容时犯了错误,它不期望双引号数字或字符< br/> 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)                                    
                                )))));

希望对你有用。

关于c# - 在 lambda 表达式 C# 中动态生成双引号字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502391/

相关文章:

c#-4.0 - 查找直方图的局部最大值/峰值和最小值/谷值

email - Elastalert使用字段值作为电子邮件警报中的地址

elasticsearch - Elasticsearch:查找在数组中没有匹配某些值的嵌套对象的文档

c# - 使用按钮 C# 控制从一个表单到另一个表单的对象属性

c# - StructureMap 生命周期范围

sql-server-2008 - 集成 SQL Service Broker 和 NServiceBus

elasticsearch - Elasticsearch匹配查询不起作用

c# - 在 WinForms WebBrowser 控件中显示 Wonky 的 Flash 视频

c# - 异步调用带有输出参数的方法

c# - 使用 Caliburn Micro 将 WebView2 绑定(bind)到 ViewModel