I would like to select objects(A) where one of property is list of objects(B) and based on criteria of object A and object B select only matching.
For example:
class Team //objects A
{
public string TeamName { get; set; }
public int StadiumName { get; set; }
public List<Player> Players { get; set; }
public string Country { get; set; }
}
class Player //objects B
{
public string Name { get; set; }
public int Age { get; set; }
public float Height { get; set; }
public float Weight { get; set; }
}
I have teams with some players and I would like to select teams where the country is England and inside that team only Players that are older than 28. For example there are three teams and every team have 16 players. Only two of those teams are from England. So the result should be list of two teams that have list of player only older than 28 (so list of players may consist of 0 to 16 players)
I wrote some linq but it either returns team with all players (16 of them) or returns null if there is no player older than 28:
teams.Where(team => team.Country == "England" && team.Players.Any(player => (player.Age > 28)));