mostafaGIS
mostafaGIS

Reputation: 11

How to Foreach() list of lists?

How to foreach this list to get coordinates (X,Y) of Two shapes from json file:

public List<List<List<List<double>>>> coordinates { get; set; }

Upvotes: 0

Views: 1393

Answers (3)

Faris Zacina
Faris Zacina

Reputation: 14274

The other answers resolve your problem, but If you need only the inner list of coordinates, i would recommend extracting that information at serialization time (preparing the document upfront) or at deserialization time using JSON.NET or ServiceStack.Text into a more manageable structure.

That will prevent the nasty code with x foreach loops or SelectMany statements, which are also loops internally because that kind of code is algorithmic hell.

Upvotes: 0

Martin Brown
Martin Brown

Reputation: 25329

The old school way would be like this:

foreach (List<List<List<double>>> listA in coordinates)
    foreach (List<List<double>> listB in listA)
        foreach (List<double> listC in listB)
            foreach (double value in listC)
            {
                // Do something with double value
            }

Upvotes: 1

quadroid
quadroid

Reputation: 8950

you can go with the following

foreach(var i in coordinates.SelectMany(x => x).SelectMany(x => x).SelectMany(x => x))
{

}

or you just box foreach calls, but i don't think a List of List of Lists etc is a good idea, what problem are you trying to solve with this?

Upvotes: 2

Related Questions