简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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