从数据库返回并非所有数据

时间:2016-07-02 15:22:46

标签: c# entity-framework wcf code-first

 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

img return

1 个答案:

答案 0 :(得分:0)

确保加载相关对象。例如:

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