Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing elements of List<List<string>>

Tags:

c#

generics

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.

like image 952
Shiv Avatar asked Apr 16 '10 04:04

Shiv


People also ask

How do you access a string in a list?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

How can you access the different elements of a list?

List is constructed by placing expressions within square brackets separated by commas. List is [10, 20]. The elements of a list can be accessed in two ways. The first way is via our familiar method of multiple assignment, which unpacks a list into its elements and binds each element to a different name.

How do you access values in an ArrayList?

An element can be retrieved from the ArrayList in Java by using the java. util. ArrayList. get() method.

How do I return a string from a list in C#?

In C# you can simply return List<string> , but you may want to return IEnumerable<string> instead as it allows for lazy evaluation.


1 Answers

You can use

if (str.Any(stud.Any(s => s == textBox3.Text)))
{
    // Do something...
}
like image 88
Ronald Wildenberg Avatar answered Oct 02 '22 09:10

Ronald Wildenberg