tags:

views:

89

answers:

3

Hello Friends. 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.

+5  A: 

You can use

if (str.Any(stud.Any(s => s == textBox3.Text)))
{
    // Do something...
}
Ronald Wildenberg
wat is s?And should I mention this code in if()
@shiv09: `bool func(string s){ return s == textBox3.Text}` translates to `s => s== textBox3.Text` while using lambda expressions. Look here http://msdn.microsoft.com/en-us/library/bb397687.aspx
TheMachineCharmer
@shiv09 it's a lambda statement, s is just a stand-in for each element in your list as it is applied to the function on the right of the => operator. It does require .NET 3 or higher though
johnc
In many answers I have seen ppl put .NET 3+ related answers. I wonder when would anyone ask what version of .NET the question is actually related to! For instance, my problems are still stuck to .NET 2 and I get 3.5 solutions :-s
Nayan
probably gonna get worse with .NET 4
Asher
+3  A: 
foreach(List<string> innerList in stud)
{
    foreach(string str in innerLst)
    {
        if(!String.IsEmptyOrNull(str) && str.Equals(textBox3.Text))
        {
            ...
        }
    }
}
Michael Shimmins
`str.Equals(...)` may have issues if `str` is `null`.
Marc Gravell
Good point - edited.
Michael Shimmins
+1 for explicitly specifying `foreach (List<string>` and `foreach( string`. When confusion hits relating to nested or complex data structures, explicitly naming the types can really help you see exactly what you are working with.
Kirk Broadhurst
I do quite like var, but only when its explicit what type you will get, either by inferring from the name of the assigner, or utterly obvious context. I probably would have used var in reality, especially since I wouldn't call the variable 'stud'. It would have looked something like foreach(var addresses in allStaffEmailAddresses) and foreach(var address in addresses). For a teaching case though I thought it would help to see the exact types.
Michael Shimmins
A: 
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 )
        //...
}
Thomas
Thanks a lot Thomas, I had to modify it somewhat but did work as I wanted it to.........Thanks man!!!! Great............