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;
}
所有项目都有id和DateMatch,但是item.Home,item.Guest,item.Result == null
答案 0 :(得分:0)
确保加载相关对象。例如:
foreach (var item in matches.Include(x => x.Home).Include(x => x.Guest).Include(x => x.Result))
{
...
}