Search code examples
c#entity-frameworkwcfcode-first

Return from database not all data


 using (var db = new ConnectToDB())
        {
            var matches = db.Matches;
            var matchesToReturn = new List<SimpleMatch>();
            foreach (var item in matches)
            {
                var match = new SimpleMatch();
                match.Id = item.Id;
                match.Home = item.Home.Name;
                match.Guest = item.Guest.Name;
                match.HomeTeamGoals = item.Result.HomeTeamGoals;
                match.GuestTeamGoals = item.Result.GuestTeamGoals;

                matchesToReturn.Add(match);

            }

            return matchesToReturn;
        }

all item has id and DateMatch, but item.Home, item.Guest, item.Result ==null

img return


Solution

  • Ensure to load the related objects. For example like this:

    foreach (var item in matches.Include(x => x.Home).Include(x => x.Guest).Include(x => x.Result))
    {
        ...
    }