1

I am using EF6 and I am trying to get a list of entities. For each entity in the list i want it to fill in the child entities in the one call.

For instance:

Order.Id
Order.ColletionOfItems

Item.Id
Item.OrderId
Item.ProductName
Item.CollectionOfOptions

Option.Name
Option.Value

using(var db = DbContext)
{   //I want to fill in everything during this call as I am using all of it in the
    //The calling function.
    OrderList = db.Orders.Select().include //This is where I am stuck
    return OrderList;
}

I want the returned collection to have all the orders, all the items associated to the individual order and all the options associated to an individual item.

How do I build my linq statement to do this? Is there a better way then the .Include("MyMajicString")? What should I actual search for because my searches have led to very few acceptable responses?

Cœur
  • 37,241
  • 25
  • 195
  • 267
DanScan
  • 831
  • 2
  • 10
  • 23
  • I believe if you have navigation properties for the child entities, you need not to even use include()(except when you have ProxyCreationEnabled=false), the moment the entity gets filled, it fills in the navigation properties too. – Nikita Shrivastava Sep 07 '15 at 02:16

1 Answers1

4

You need to use the Include extension method:

using(var db = new YourContext())
{   
    var OrderList = db.Orders.Include(o=>o.ColletionOfItems.Select(i=>i.CollectionOfOptions));
    return OrderList;
}

You can also use the DbQuery.Include method which receive as parameter the path of the nav. properties you want to load as an string:

using(var db = new YourContext())
{   
    var OrderList = db.Orders.Include("ColletionOfItems.CollectionOfOptions");
    return OrderList;
}

But is better use the first one because is strongly typed. With this second method you can make a mistake typing the name of one of the properties or you could change the name of the nav. properties in your model in the future and forget rename the property in the path.

Another thing, be careful loading large amounts of data. With that query, you are going to load all orders and the related properties, so, try to filter first (calling the Where method after the Include call) before load the info you need to your application.

ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • I have VS 2012 and EF6 but I cannot find the `DbExtensions.Include` method. From the link, looks like it's just for EF5, so what if we need something similar in EF6? – Hopeless Sep 07 '15 at 00:57
  • Use the same method, check this [link](http://stackoverflow.com/questions/23640945/where-is-include-dbextension-for-ef-6-0) – ocuenca Sep 07 '15 at 00:58
  • There is the same method named Include but it's under `QueryableExtensions` (maybe this is new in EF6) but not any found under `DbExtensions`. – Hopeless Sep 07 '15 at 01:01
  • That worked for on level but the second level did not work. The .Include(o=>o.CollectionOfItems.Select I don't have the "Select" extension available. (nor the .Include either). Am I missing a reference? – DanScan Sep 07 '15 at 03:43
  • 1
    Have you made reference to `System.Data.Entity`? – ocuenca Sep 07 '15 at 04:24