C# deep connected query

c# entity-framework-core

Question

Currently I have the below code which returns a list which goes through 4 tables Tournament -> Tournament_PlayerIDs -> PlayerIDs_Players -> Players. What I am trying to do is return a list of tournaments in which a player has enrolled with one of their ID's to play in.

    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();
    }

What I would like is something like

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();
    }

Model Structures

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
12/6/2018 7:36:03 PM

Accepted Answer

I believe that what you're looking for is called SelectMany. I believe the example in the link covers what you want (get all tournaments a given player participated)

1
12/6/2018 7:28:41 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow