0

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; }
4
  • 2
    Where does your problem deal with JSON. Correct your tags please ;) Commented Dec 5, 2014 at 12:39
  • Damn that is a long list list list list Commented Dec 5, 2014 at 12:44
  • 1
    Call SelectMany three times. Commented Dec 5, 2014 at 12:44
  • Out of curiosity.. can you post the JSON file? that is some really nasty nesting. Commented Dec 5, 2014 at 12:48

3 Answers 3

2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

That's once too often, you only need 3 to reduce 4D to 1D.
1

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
            }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.