Can anyone let me know how can access an element of a list that has been added to a list of list. I'll mention the code.
List<string> str开发者_开发知识库 = new List<string>();
List<List<string>> stud = new List<List<string>>();
A method has been defined that inserts data into str and after the method gets over.
stud.Add(str);
The method and stud.Add(str) is on a button click...... so, each time str contains different data.......
the problem is I want to search in whole of stud i.e. all the str created, whether str[0]==textBox3.Text;
I'm confused in the For loops...how to reach to all the str[0] in stud to verify the condition.
You can use
if (str.Any(stud.Any(s => s == textBox3.Text)))
{
// Do something...
}
foreach(List<string> innerList in stud)
{
foreach(string str in innerLst)
{
if(!String.IsEmptyOrNull(str) && str.Equals(textBox3.Text))
{
...
}
}
}
var stud = new List<List<string>>();
foreach( var list in stud )
{
foreach( item in list )
{
if ( item == textBox3.Text )
//...
}
}
If you only want to search the first item in the first list then you could do:
if ( stud.Count > 0 )
{
var list = stud[0]
if ( list.Count > 0 && list[0] == textbox3.Text )
//...
}
The SelectMany
method will flatten your list elements:
// untested
var all = stud.SelectMany(...)
.Where(...)
精彩评论