2017-03-15 39 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))); 
相关问题