简体   繁体   English

实体框架获取带有子实体的实体

[英]Entity framework Get entity with child entities

I need a list of items. 我需要物品清单。 Every item has a list of files. 每个项目都有一个文件列表。 What I need is all the items for a certain date and every item needs to include its files. 我需要的是特定日期的所有项目,每个项目都需要包括其文件。 So far I have 到目前为止,我有

var result = (from i in db.Context.Items.AsNoTracking()
                          where EntityFunctions.TruncateTime(i.Date) == date.Date
                          select i).ToList();

            return result; 

This gives me all the items and it's basic properties like name and id and such. 这给了我所有项目,它是基本属性,例如name和id等。 But the list of files is empty. 但是文件列表为空。 Anyone can help me with this? 有人可以帮我吗?

Try this: 尝试这个:

var result = (from i in db.Context.Items.AsNoTracking()
                      where EntityFunctions.TruncateTime(i.Date) == date.Date
                      select i).Include(i=>i.Files).ToList();

        return result;

Make sure that you have System.Data.Entity referenced to get this .Include extension method overload. 确保已引用System.Data.Entity以获取此.include扩展方法重载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM