Reputation: 59
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)));
Upvotes: 0
Views: 583
Reputation: 3013
Another option is to have a GetPlayers(Func<Player,bool> condition)
method which you can use to get filtered players:
class Team
{
public string TeamName { get; set; }
public int StadiumName { get; set; }
public List<Player> Players { get; set; }
public string Country { get; set; }
public IEnumerable<Player> GetPlayers(Func<Player, bool> condition)
{
return Players.Where(condition);
}
}
This is a better solution than returning a new object with filtered players because filtered players are not getting lost, and you can access them from Players
property.
Upvotes: 3
Reputation: 150108
Your requirements essentially necessitate creating a new Team
object with a reduced set of players (or alternatively permanently removing the younger players from the original Team
object).
var teamsEnglandOnlyOver28Years =
teams
// Only teams from England
.Where(t => t.Country == "England")
// Create a new instance with only the older players
.Select(t => new Team()
{
TeamName = t.TeamName,
StadiumName = t.StadiumName,
Players = t.Players.Where(p => p.Age > 28).ToList(),
Country = t.Country
});
This may or may not be what you actually intend. It seems a bit awkward because, for example, the team name is the same as for the original team but the roster is reduced.
Upvotes: 6