0

I have this JSON object

{
   "manufacturers":[
      {
         "name":"Chevy",
         "cars":[
            {
               "name":"Corvette",
               "colors":[
                  "black",
                  "midnight blue",
                  "silver",
                  "inferno red",
                  "atomic orange"
               ]
            },
            {
               "name":"Camaro",
               "colors":[
                  "black",
                  "blue",
                  "yellow",
                  "red"
               ]
            }
         ]
      },
      {
         "name":"Ford",
         "cars":[
            {
               "name":"GT",
               "colors":[
                  "light blue",
                  "black"
               ]
            },
            {
               "name":"Mustang",
               "colors":[
                  "black",
                  "blue",
                  "dark green",
                  "silver"
               ]
            },
            {
               "name":"Focus RS",
               "colors":[
                  "light blue",
                  "dark blue",
                  "black",
                  "silver"
               ]
            }
         ]
      },
      {
         "name":"Tesla",
         "cars":[
            {
               "name":"S",
               "colors":[
                  "white",
                  "black",
                  "red",
                  "silver",
                  "blue"
               ]
            },
            {
               "name":"3",
               "colors":[
                  "white",
                  "black",
                  "red",
                  "silver",
                  "blue"
               ]
            },
            {
               "name":"X",
               "colors":[
                  "white",
                  "black",
                  "red",
                  "silver",
                  "blue"
               ]
            },
            {
               "name":"Y",
               "colors":[
                  "white",
                  "black",
                  "red",
                  "silver",
                  "blue"
               ]
            }
         ]
      }
   ]
}

I have to find all card where manufacturer is Ford:

var fordCars = apiModel.Manufacturers.Find(o => o.Name == "Ford");

It gives me the second object. How do I get the names of cards, namely GT, mustang and focus from this?

// In Progress Can I do something like this?

var fordCars = apiModel.Manufacturers.Find(o => o.Name == "Ford").Cars.FindAll(o => o.);
0

1 Answer 1

3

First of all, if you need all cars where the manufacturer is Ford, then you have to use .Where() instead of .Find().

var fordCars = apiModel.Manufacturers.Where(o => o.name == "Ford");

Now if you want list of all car names, then use .SelectMany() after .Where() clause,

var carNames = fordCars
    .SelectMany(x => x.Cars)  //Convert sequence of car object
    .Select(x => x.Name);     //Select only Name from car object 

Why should we use .Where() instead of .Find()?

Find():

  • Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List.

Where():

  • Filters a sequence of values based on a predicate and returns all the filtered data

Try Online

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

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.