Cocoa Dev
Cocoa Dev

Reputation: 9541

C# What does List<List<string>> mean?

What is List<List<string>> and how do I get the data out (as Strings)?

Upvotes: 3

Views: 25288

Answers (8)

karthik kasubha
karthik kasubha

Reputation: 424

class Program
{
static void Main(string[] args)
{
List<string> CountryOne = new List<string> { "City1", "City2", "City3" };

List<string> CountryTwo = new List<string> { "City4", "City5", "City6" };

List<string> CountryThree = new List<string> { "City7", "City8", "City9" };

List<List<string>> countries = new List<List<string>>();

countries.Add(CountryOne);
countries.Add(CountryTwo);
countries.Add(CountryThree);

Console.WriteLine("Few cities from each country");
Console.WriteLine();
foreach (List<string> country in countries)
{
    foreach(string city in country)
    {
        Console.WriteLine(city);
    }

    Console.WriteLine("----------------------");
}
}
}

Upvotes: 0

Niladri Biswas
Niladri Biswas

Reputation: 4171

It implies List of List of strings.

e.g.

//Add data in inner list
var lstString = new List<string>();
Enumerable.Range(1, 10)
  .ToList()
  .ForEach(i => lstString.Add(string.Concat("string", i)));

//Add data in outer list
List<List<string>> lstStrings = new List<List<string>>();
Enumerable.Range(1, 5)
  .ToList()
  .ForEach(j => lstStrings.Add(lstString));

//To fetch data (using lambda)
lstStrings.ForEach(i => i.ForEach(j => Console.WriteLine(j)));

//To fetch data using Linq

(from x in lstStrings
    from y in x
    select y).ToList().ForEach(j => Console.WriteLine(j));

Upvotes: 2

Belmiris
Belmiris

Reputation: 2805

List<List<string>> lists;
...
foreach (List<string> list in lists)
{
    foreach (string s in list)
    {
        Console.WriteLine(s);
    }
}

Upvotes: 11

Vamsi
Vamsi

Reputation: 4253

It's exactly what it reads it is a List of (List of strings), you can use SelectMany to flatten the object to just List<string>

List<List<String>> objListOfListString = new List<List<string>>();
//Some code here that fills the above object
List<string> justAListOfString = objListOfListString.SelectMany(s => s).ToList();

Upvotes: 2

code4life
code4life

Reputation: 15794

For a List<List<string>>, you can do this:

// let's assume listOfListOfString = List<List<string>>
foreach(var list in listOfListOfString)
{
    foreach(var string in list)
    {
        // ....
    }
}

Upvotes: 0

Andre Calil
Andre Calil

Reputation: 7692

How to get the values:

foreach(List<string> CurrentList in DoubleList)
{
  foreach(string TheString in CurrentList)
  {
    //...
  }
}

What does it mean: well, it's a little awkward. As others said, it's a list of a list of strings. It behaves much like a double-indexed array (a matrix), like string[i][j]. However, while an array is pre-fixed, a list is not.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500675

It's a list of lists of strings. Each element of the "outer" list is a list of strings. The simplest way to "flatten" the list is to use LINQ:

var flattened = listOfLists.SelectMany(list => list);

foreach (var value in flattened)
{
    Console.WriteLine(value);
}

Upvotes: 20

Oded
Oded

Reputation: 499012

It is a list of lists of strings.

To get to the actual strings, you need to iterate over the list of lists, then for each list of strings, iterate over it to get the strings.

foreach(var stringList in myList)
{
   foreach(var aString in stringList)
   {
      // do someting with aString
   }
}

Upvotes: 7

Related Questions