I am trying to build custom result set with LINQ. Currently, I 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 Teams to be a new custom type. I want the type to have a field called IsChecked, TeamId, and TeamName. However, each Team currently only has TeamId and TeamName. How do I get an IsChecked property added within this LINQ statement? Is there a way to nest "select new" statements? If so, how?
Thanks?
Sure, just add that 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 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()
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.