0

I have list of objects that looks like this:

teams: [
  {
    "teamName": "Bayern",
    "players":[
      {
        "name": "Neuer",
        "status": "available"
      },
      {
        "name": "Muller",
        "status": "unavailable"
      },
      {
        "name": "Pavard",
        "status": "unknown"
      }
    ]
  },
  {
    "teamName": "Borrusia",
    "players":[
    {
        "name": "Haller",
        "status": "available"
      },
    {
        "name": "Modeste",
        "status": "unavailable"
      }
    ]
  }
]

So in short i have list of teams where you can find teamName attribute and list of players in that team. Every player has status property which is Enum. Is it possible to filter the nested list players so i am left with only the players that have status available?

I want to get at the end object like this:

teams: [
  {
    "teamName": "Bayern",
    "players":[
      {
        "name": "Neuer",
        "status": "available"
      }
    ]
  },
  {
    "teamName": "Borrusia",
    "players":[
    {
        "name": "Haller",
        "status": "available"
      }
    ]
  }
]

1 Answer 1

2

Try with this code, just use a method 'Where' and you will get only players availables:

var result = teams.Select(team => new 
{ 
  teamName = team.teamName,
  players = team.players.Where(player => player.status == "available")
});
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.