Hi All,

I have a nest list and i need to get the indivdual columns
i can do a foreach and the sublist has the 4 arrays but i cant access each columns

e.g. the first column is 1
                                         2
  the second column is 5 
                                         8

Thanks for the help.

 List<List<float>> myList = new List<List<float>>();
            myList.Add(new List<float> { 1, 2 });
            myList.Add(new List<float> { 5, 8 });
            myList.Add(new List<float> { 4, 44 });
            myList.Add(new List<float> { 9, 7 });

            foreach (List<float> sublist in myList)
            {

                foreach (float item in sublist)
                {

                }
            }

That code works as is for me. I added a console.writeLine into your inner foreach and all of the values got output.
What is happening when you run it?

thanks for the reply.
yes that is whats happening but i only want to print the first row like 1, 2 or prints just the second row 5,8.
i tried doing a nested loop etc and it didnt work.

I know you could have 4 seperate lists and print them indivdually.
Thanks again

     foreach (List<float> sublist in myList)
            {
                foreach (float item in sublist[1])
                {
                }
            }

If you only want to print the first row each time then the second loop isn't needed. You can target sublist[0] immediately, or whatever column you do want.
If the column will change then you need to determine which one you want prior to the foreach and set a variable e.g.

int col = 2;
foreach(List<float> sublist in myList) {
    // access sublist[col]
}

i tried implementing that but it doesnt work.

something like this but a better way of programming it

 List<List<float>> myList = new List<List<float>>();
            myList.Add(new List<float> { 1, 2 });
            myList.Add(new List<float> { 5, 8 });
            myList.Add(new List<float> { 4, 44 });
            myList.Add(new List<float> { 9, 7 });

            int counter = 0;
            int counter2 = 0;
            int counter3 = 0;

            foreach (List<float> sublist in myList)
            {

                foreach (float item in sublist)
                {
                    counter++;

                    if (counter <= 2)
                    {
                        Console.WriteLine(item);
                    }
                }

                foreach(float cyan in sublist)
                {
                    counter2++;
                    if(counter<=4 && counter>2)
                    {
                        Console.WriteLine("test"+cyan);
                    }
                }

                foreach (float cyan in sublist)
                {
                    counter3++;
                    if (counter <= 6 && counter > 4)
                    {
                        Console.WriteLine("test2" + cyan);
                    }
                }

Still don't know exactly your intentions. Seems you try to mix the concept of a row and a column. So I'm doing a wild guess.
I consider that in your example your list type has 4 rows and 2 columns.
As a List supports indexing just do this:

static void PrintRow(List<List<float>> aList, int rowtoPrint) //zero based
{
    for (int i = 0; i < aList[rowtoPrint].Count(); i++)
    {
        Console.Write(aList[rowtoPrint][i]);
        Console.Write(' '); //separate with space
    }
    Console.WriteLine();
}

And call it this way:

Console.WriteLine("Printing the third row:");
PrintRow(myList, 2);

Hope this is what you want.
And BTW stop saying that it does not work. Rather tell us WHAT is not working(behaviour, error messages etc.)
This would really help in getting answers, believe me.

thats exactly it . thanks for the advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.