2016-02-19 51 views
2

我有數字,我在列表中concatenating double quoted動態添加和投入的代碼如下在lambda表達式C#動態生成雙引號字符串

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

我會感謝您的幫助!謝謝

+0

它應該採用''RespId':{「5」,「6」,「7」,「8」,...'請參見圖片 – Dipesh

+0

爲什麼要將字符串數組轉換爲包含負載的字符串逃脫報價?只需傳遞未轉義的字符串數組即可。 –

+0

上面是一個用於'ElasticSearch'的lambda表達式查詢,'Elasticsearch'在我們用雙引號字符串發送時拋出一個異常 – Dipesh

回答

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)          
           ))))); 

我希望它能爲你工作。

+0

Thanx再次@maxCoder讓我實現它,並儘快恢復你:) – Dipesh

+0

嘿它比thanx好很多代碼和簡要說明:) – Dipesh

+0

謝謝,我很高興你想通了,不要忘記標記爲答案 – MaxCoder

相關問題