2017-03-15 53 views
-2

我想驗證IF字符串存在於List<string>內。但List<string>List<List<string>> ..如何檢查字符串是否在列表中的列表中

請檢查我的code下面。它會拋出一個ArgumentException

loadTestList = new List<List<string>>(); 

loadTestList.ElementAt(loadTestSelect.SelectedIndex - 1).Contains(scorecardName); 

必須注意loadTestSelect下拉/選擇用戶從選擇。用戶選擇將被驗證的List<string>

scoreCardName是我想搜索的字符串。

非常感謝!

+1

它如何與JavaScript和jQuery相關? –

+0

只爲您的問題選擇匹配的標籤。人們會回答你沒有C#知識的人。 –

+0

@Sagar V我的驗證功能在客戶端。帶有上面Contains的代碼包含在'<% %>'標籤中 – JPaulPunzalan

回答

1

試試這個:

if(loadTestList.Any(x => x.Contains(scorecardName)) 
{ 
    // Proceed specified item is present in one sublist 
} 

或者,如果您要檢查包含基於所選擇的指數從loadTestSelect

if(loadTestList[loadTestSelect.SelectedIndex].Contains(scorecardName)) 
{ 
    // Proceed specified item is present 
} 
0

指定的子列表下面只嘗試的LINQAny()兩次

var loadTestList = new List<List<string>>() { 
    new List<string>() {"a", "b", "c"}, 
    new List<string>() {"la-la-la"}, 
    new List<string>(), 
    null, 
    new List<string>() {"test", null, "sample"}, 
}; 

string scorecardName = "am"; // should be found in the "sample" 

var exists = loadTestList 
    .Any(list => list != null && 
       list.Any(item => item != null && 
           item.Contains(scorecardName))); 
相關問題