Venedchuk
Venedchuk

Reputation: 35

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

Upvotes: 0

Views: 66

Answers (1)

Udo
Udo

Reputation: 459

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))
{
    ...
}

Upvotes: 1

Related Questions