我有一个如下所示的查询:
var tournamentMatches = community.Tournaments.
SelectMany(x => x.Rounds.
SelectMany(y => y.Matches)).Where(j => j.Away.Members.Count > 0).
Select(t => new TeamLeagueMatch()
{
HomeParticipantId = t.HomeParticipant,
PlayedOn = t.PlayedOn,
Result = t.Result,
}).ToList();
我想要访问列表的基础部分,以获取锦标赛的名称,所以在 Result = t.Result
下面我希望能够放置:
Name = x.Name
但是,它无法识别此级别的 x 变量。有没有一种简单的方法来获取我的 name 属性而无需诉诸长期涉及的 foreach 循环?
这些类看起来是这样的:
public class Tournament
{
public string Name { get; set; }
public IList<TournamentRound> Rounds { get; set; }
}
public class TournamentRound
{
public DateTime? PlayBy { get; set; }
public IList<Match> Matches { get; set; }
}
public class Match
{
public MatchResult Result { get; set; }
public MatchSide Home { get; set; }
public MatchSide Away { get; set; }
}
public class MatchSide
{
public IList<string> Members { get; set; }
}
最佳答案
使用查询语法:
var tournamentMatches = (from tournament in community.Tournaments
from round in tournament.Rounds
from match in round.Matches
where match.Away.Members.Count > 0
select new TeamLeagueMatch
{
HomeParticipantId = match.HomeParticipant,
PlayedOn = match.PlayedOn,
Result = match.Result,
Name = tournament.Name
}).ToList();
关于c# - 访问 Linq 查询的基本列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39344647/