Eljey Shikaku
Eljey Shikaku

Reputation: 55

How to get the data inside a List<array>

example...

List<array> thisListOfArray = new List<array>();
List<string> thisArrayA= new List<string>();
List<string> gMaintenanceB = new List<string>();
thisArrayA.Add("ItemA1");
thisArrayA.Add("ItemA2");
thisArrayA.Add("ItemA3");
thisArrayB.Add("ItemB1");
thisArrayB.Add("ItemB2");
thisArrayB.Add("ItemB3");
thisListOfArray.Add(thisArrayA.ToArray());
thisListOfArray.Add(thisArrayB.ToArray());

I want to get every value that I have inputted inside the thisListOfArray.

Upvotes: 0

Views: 125

Answers (3)

Salah Akbari
Salah Akbari

Reputation: 39976

Change your code like this:

List<List<string>> thisListOfArray = new List<List<string>>();
List<string> thisArrayA = new List<string>();
List<string> gMaintenanceB = new List<string>();
thisArrayA.Add("ItemA1");
thisArrayA.Add("ItemA2");
thisArrayA.Add("ItemA3");
gMaintenanceB.Add("ItemB1");
gMaintenanceB.Add("ItemB2");
gMaintenanceB.Add("ItemB3");
thisListOfArray.Add(thisArrayA);
thisListOfArray.Add(gMaintenanceB);

foreach (var itm in thisListOfArray.SelectMany(item => item))
{
    MessageBox.Show(itm);
}

Upvotes: 1

Kunal Khatri
Kunal Khatri

Reputation: 471

you can get it following way. here is the code

List<string[]> thisListOfArray = new List<string[]>();
List<string> thisArrayA = new List<string>();
List<string> thisArrayB = new List<string>();
thisArrayA.Add("ItemA1");
thisArrayA.Add("ItemA2");
thisArrayA.Add("ItemA3");
thisArrayB.Add("ItemB1");
thisArrayB.Add("ItemB2");
thisArrayB.Add("ItemB3");
thisListOfArray.Add(thisArrayA.ToArray());
thisListOfArray.Add(thisArrayB.ToArray());

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

foreach (var strArray in thisListOfArray)
{
    foreach (var str in strArray)
    {
        lstNewstring.Add(str);
    }
}

MessageBox.Show(lstNewstring.Count.ToString());

Upvotes: 1

Acha Bill
Acha Bill

Reputation: 1255

If you have:

  • List<string[]> listOfArray
  • string[] array

Add the array to the List with listOfArray.Add(array); Not sure where you'll be getting array from.

Upvotes: 0

Related Questions