0

I would like to use eager loading for load related entities and I see this page:

In this example I can see that there are two ways to get the related entities:

var princesses1 = context.Princesses
                          .Include(p => p.Unicorns)
                          .ToList();


var princesses1 = context.Princesses
                          .Include("Unicorns")
                          .ToList();

The first way is to use the lambda expression (I think that correct name is that, if not, correct me), and the second way is to use a string with the name of the related entity.

In my case, I can use the second, because in the firt way, when I can't get the property of the related entity in the lambda expression. I use this code:

IQueryable<Customers> myQuery;
myQuery = myContext.Customers.Include("Orders");

But if I try to use the second way:

IQueryable<Customers> myQuery;
myQuery = myContext.Customers.Include(c=>c.?????);

I can't select the Orders property.

Why?

Marc
  • 3,683
  • 8
  • 34
  • 48
Álvaro García
  • 18,114
  • 30
  • 102
  • 193

2 Answers2

2

You should use System.Data.Entity in order to be able to use lambda expression in Include method:

using System.Data.Entity;
cuongle
  • 74,024
  • 28
  • 151
  • 206
1

Add this to the top of your file:

using System.Data.Entity;

This includes a reference to a DbExtensions class which provides the include extension method.

Sethcran
  • 798
  • 5
  • 11