Nested anonymous type in LINQ
I am trying to create a custom result set using LINQ. I currently have the following:
List<Team> teams = Team.FindAll();
List<Player> players = Player.FindAll();
var results = (from player in players
select new
{
PlayerId = player.Id,
PlayerName = player.Name,
Teams = teams.Where(t => player.TeamsIds.Contains(t.TeamId)).ToList()
});
I want commands to be a new custom type. I want the type to have a field named IsChecked, TeamId and TeamName. However, each team has a TeamId and TeamName. How do I get the IsChecked property added to this LINQ statement? Is there a way to enclose a "selection of new" statements? If so, how?
Thank?
+3
source to share
1 answer
Of course, just add this as a projection:
var results = (from player in players
select new
{
PlayerId = player.Id,
PlayerName = player.Name,
Teams = teams.Where(t => player.TeamsIds.Contains(t.TeamId))
.Select(t => new {
t.TeamId,
t.TeamName,
IsChecked = ???
}
.ToList()
});
or using the query syntax
var results = (from player in players
select new
{
PlayerId = player.Id,
PlayerName = player.Name,
Teams = (from t in teams
where player.TeamsIds.Contains(t.TeamId)
select new {
t.TeamId,
t.TeamName,
IsChecked = ???
}
).ToList()
});
+8
source to share