开发者

Accessing elements of List<List<string>>

开发者 https://www.devze.com 2022-12-26 20:41 出处:网络
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.

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(...)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号