Linq - Consultation in the third level list

0

I have a list of a class (A) that contains a list of another class (B) and that in turn contains another list of another class (C). I need to filter the initial list (A) from the value of a property of the class (C). The example would be something like this:

using System;
using System.Collections.Generic;

namespace PruebaLinq
{
  public class Leage
  {
     public string NameLeage { get; set; }
     public List<Team> teams { get; set; }
  }

  public class Team
  {
     public string NameTeam { get; set; }
     public List<Player> players { get; set; }
  }

  public class Player
  {
     public string NamePlayer { get; set; }
     public int Age { get; set; }
  }


  class Program
  {
     static void Main(string[] args)
     {
        #region Inicializar lista
        List<Leage> lista = new List<Leage>
        {
           new Leage
           {
              NameLeage = "Leage_1",
              teams = new List<Team>
              {
                 new Team
                 {
                    NameTeam = "Team_1_1",
                    players = new List<Player>
                    {
                       new Player
                       {
                          NamePlayer = "Player_1_1_1",
                          Age = 20
                       },
                       new Player
                       {
                          NamePlayer = "Player_1_1_2",
                          Age = 22
                       }
                    }
                 },
                 new Team
                 {
                    NameTeam = "Team_1_2",
                    players = new List<Player>
                    {
                       new Player
                       {
                          NamePlayer = "Player_1_2_1",
                          Age = 19
                       },
                       new Player
                       {
                          NamePlayer = "Player_1_2_2",
                          Age = 21
                       }
                    }
                 }
              }
           },
           new Leage
           {
              NameLeage = "Leage_2",
              teams = new List<Team>
              {
                 new Team
                 {
                    NameTeam = "Team_2_1",
                    players = new List<Player>
                    {
                       new Player
                       {
                          NamePlayer = "Player_2_1_1",
                          Age = 19
                       },
                       new Player
                       {
                          NamePlayer = "Player_2_1_2",
                          Age = 22
                       }
                    }
                 },
                 new Team
                 {
                    NameTeam = "Team_2_2",
                    players = new List<Player>
                    {
                       new Player
                       {
                          NamePlayer = "Player_2_2_1",
                          Age = 19
                       },
                       new Player
                       {
                          NamePlayer = "Player_2_2_2",
                          Age = 21
                       }
                    }
                 }
              }
           },
           new Leage
           {
              NameLeage = "Leage_3",
              teams = new List<Team>
              {
                 new Team
                 {
                    NameTeam = "Team_3_1",
                    players = new List<Player>
                    {
                       new Player
                       {
                          NamePlayer = "Player_3_1_1",
                          Age = 20
                       },
                       new Player
                       {
                          NamePlayer = "Player_3_1_2",
                          Age = 22
                       }
                    }
                 },
                 new Team
                 {
                    NameTeam = "Team_3_2",
                    players = new List<Player>
                    {
                       new Player
                       {
                          NamePlayer = "Player_3_2_1",
                          Age = 19
                       },
                       new Player
                       {
                          NamePlayer = "Player_3_2_2",
                          Age = 20
                       }
                    }
                 }
              }
           }
        };
        #endregion

        //Necesito obtener las ligas en que haya algún jugador de 20 años.
        //lista = ????????;

        Console.ReadLine();
     }
  }
}

As I indicated in the commentary, the list should contain only the Leage_1 and Leage_3 links

Greetings and thanks for the help.

    
asked by Carlos 08.06.2017 в 12:43
source

1 answer

3

You have to look for the leagues that have some team with a player of 20 years:

    var filterList = lista.Where(l => l.teams.Any(t => t.players.Any(p => p.Age == 20)));
    foreach (var league in filterList)
    {
        Console.WriteLine($"Liga con jugador de 20 años: {league.NameLeage}");
    }
    
answered by 08.06.2017 / 12:53
source