C#深度连接查询

时间:2018-12-06 19:04:52

标签: c# entity-framework-core

当前,我有以下代码,该代码返回经过4个表的列表锦标赛->锦标赛玩家ID->玩家ID_玩家->玩家。 我要做的是返回一个锦标赛列表,其中一个玩家注册了一个ID即可参加比赛。

    public IEnumerable<Tournament> GetAllTournaments()
    {
        return _appDbContext.Tournaments
       .Include(f => f.PlayerIDIDs)
       .ThenInclude(PlayerIDIDs => PlayerIDIDs.Player)
       .ThenInclude(Player=> Player.Player)
       .Include(f => f.TournamentGame)
       .Include(f => f.TournamentFormat).ToList();
    }

我想要的是类似的东西

public IEnumerable<Tournament> GetAllTournaments(string id)
    {
        return _appDbContext.Tournaments
       .Include(f => f.PlayerIDIDs)
       .ThenInclude(PlayerIDIDs => PlayerIDIDs.Player)
       .ThenInclude(Player=> Player.Player)
       .Include(f => f.TournamentGame)
       .Include(f => f.TournamentFormat)
       .Where (f.PlayerIDIDs.Player.Player.Id == id)
       .ToList();
    }

模型结构

public class Tournament
{
    [Key]
    public int TournamentID { get; set; }
    [Required]
    [StringLength(100, ErrorMessage = "Game name is required")]
    public string TournamentName { get; set; }
    [Required]
    [StringLength(5000, ErrorMessage = "Short Desc is required")]
    public string TournamentDescription { get; set; }
    public bool IsMajorTournament { get; set; }
    public decimal TournamentFee { get; set; }
    public DateTime TournamentStartTime { get; set; }

    //Foregin Keys
    public int? GameID { get; set; }
    public Game TournamentGame { get; set; }
    public int? FormatID { get; set; }
    public Format TournamentFormat { get; set; }
    public ICollection<PlayerID_Tournament> PlayerIDIDs { get; } = new List<PlayerID_Tournament>();

}

public class PlayerID_Tournament
{
    public int PlayerIDID { get; set; }
    public PlayerID Player { get; set; }
    public int TournamentID { get; set; }
    public Tournament Tournament { get; set; }
}

public class PlayerID
{
    //Identity for Player ID
    [Key]
    public int PlayerIDID { get; set; }
    //Id for Game such as DCI number
    public string PlayerGameID { get; set; }
    //game info
    public int GameId { get; set; }
    public Game Game { get; set; }
    //user info
    public string PlayerId { get; set; }
    public ApplicationUser Player { get; set; }
    public ICollection<PlayerID_Tournament> Tournaments { get; } = new List<PlayerID_Tournament>();
}

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set;}
    public string LastName { get; set; }
    public ICollection<PlayerID> PlayerIDs { get; set; }
}

1 个答案:

答案 0 :(得分:1)

我相信您正在寻找的东西叫做SelectMany。我相信链接中的示例涵盖了您想要的内容(让特定玩家参加所有比赛)