0

I want to query the database and get a sport object containing all inner lists, but for some reason I'm missing, the select only goes 2 levels deep in lists, any deepers and the lists property have a value of null, example of the structure

public class Sports
{
    public int id { get; set; }
    public string name { get; set; }
    public List<League> leagues { get; set; }
}
public class League
{
    public int id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public List<Team> teams { get; set; }
}
public class Team
{
    public int id { get; set; }
    public string name { get; set; }
    public string successrate { get; set; }
    public List<Player> players { get; set; }
}
public class Player
{
    public int id { get; set; }
    public string name { get; set; }
    public int age { get; set; }
}

I created property in MyAppContext file as this

public DbSet<Sports> sports { get; set; }

now when I call an item using select or linq or any other way I tried, the sport object is always 2 Dimensional, meaning it doesn't go deeper than two levels in nested lists! Example of result using var sport=db.Sports.First() the result is {"id":1,"name":"Football","leagues":null} or if I used select()

   var sportQuery = db.sports.Select(
     s=>new Sports(){
        id=s.id,
        leagues=s.leagues,
        name=s.name
    }).First();

I still don't get full information {"id":1,"name":"Football","leagues":[{"id":1,"name":"fc","description":"Some Leauge","teams":null},{"id":2,"name":"al","description":"League","teams":null}]} why is that! and how to get full object like this

{"id":1,"name":"Football","leagues":[{"id":1,"name":"fc","description":"Some Leauge","teams":[{"id":1,"name":"real madrid","successrate":null,"players":[{"id":1,"name":"Cristiano Ronaldo","age":21},{"id":2,"name":"Iniesta","age":38}]},{"id":2,"name":"Barcelona","successrate":null,"players":[{"id":1,"name":"Cristiano Ronaldo","age":21},{"id":2,"name":"Iniesta","age":38}]}]},{"id":2,"name":"al","description":"League","teams":[{"id":1,"name":"real madrid","successrate":null,"players":[{"id":1,"name":"Cristiano Ronaldo","age":21},{"id":2,"name":"Iniesta","age":38}]},{"id":2,"name":"Barcelona","successrate":null,"players":[{"id":1,"name":"Cristiano Ronaldo","age":21},{"id":2,"name":"Iniesta","age":38}]}]}]}

I've been stuck for days, Please any help would be much appreciated

Mosta
  • 147
  • 10
  • Possible duplicate of [How to include() nested child entity in linq](https://stackoverflow.com/questions/24120039/how-to-include-nested-child-entity-in-linq) – MattD Oct 17 '18 at 17:02

2 Answers2

0

problem solved using Include() and thenInclude() https://learn.microsoft.com/en-us/ef/core/querying/related-data however, that doesn't explain why Select() only loads one list property deep, also it seems like i should be able to load object with select only or linq

Mosta
  • 147
  • 10
0

Try following :

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            DataBase db = new DataBase();

            var sportQuery = db.sports.Select(s=> s.leagues.Select(l => l.teams.Select(t => t.players.Select(p => new {
                        playerid =  p.id,
                        playerName = p.name,
                        playerAge = p.age,
                        teamId = t.id,
                        teamName = t.name,
                        teamSucessrate = t.successrate,
                        leagueId= l.id,
                        leagueName= l.name,
                        leagueDescription = l.description,
                        sportId = s.id,
                        sportName = s.name
            }))
            .SelectMany(p => p))
            .SelectMany(t => t))
            .SelectMany(l => l)
            .ToList();
        }
    }
    public class DataBase
    {
        public List<Sports> sports { get; set;}
    }

    public class Sports
    {
        public int id { get; set; }
        public string name { get; set; }
        public List<League> leagues { get; set; }
    }
    public class League
    {
        public int id { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public List<Team> teams { get; set; }
    }
    public class Team
    {
        public int id { get; set; }
        public string name { get; set; }
        public string successrate { get; set; }
        public List<Player> players { get; set; }
    }
    public class Player
    {
        public int id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20