c# - 访问 Linq 查询的基本列表

标签 c# .net linq

我有一个如下所示的查询:

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/

相关文章:

c# - MSTEST 委托(delegate)人许可

c# - ToString() 在字符串上使用时会产生新字符串吗?

.net - 无法使用 ASP.NET4 隐藏 CSS 中的元素

c# - 在可空和非可空 int 上调用 'join' 时类型推断失败

c# - 使用 Linq 从数组构建字典

c# - 从 Azure Blob 存储读取数据时 OpenReadAsync() 出现奇怪的结果

c# - 有没有办法更改 .net mvc bin 目录位置?

c# - Office Web Apps 和 WOPI

c# - 延迟然后执行任务

linq union合并子列表