92

I've seen many articles about how to overcome this matter, all related to CTP4, Or adding my own extension methods.

Is there an "official" EF4 included way to use lambda expressions inside include (for both first level relations and also 2nd and more level) or is it eventually was not included in the RTM ?

It there is one - I would be glad to learn how to do it, as using lambda expression in my code now (with #system.data.entity #system.data.linq) still gives me:

Cannot convert lambda expression to type 'string' because it is not a delegate type on:

var customers = from c in
context.Customers.Include(c=>c.Phone)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Dani
  • 14,639
  • 11
  • 62
  • 110

3 Answers3

240

The RTM version of Entity Framework 4.1 actually includes extension methods in the EntityFramework.dll file, for eager loading with lambda through the Include function. Just include the DLL in your project and you should be able to write code like:

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

Remember to add an Import/Using statement to include the System.Data.Entity namespace. Otherwise the compiler cannot find the extension methods. E.g:

using System.Data.Entity;

See this ADO.NET team blog article for more information.

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
cecilphillip
  • 11,446
  • 4
  • 36
  • 40
  • 1
    While the link to the ADO.net blog is still relevant, the EF6 reference/guide for the same topic is: [Loading Related Entities](https://msdn.microsoft.com/en-us/data/jj574232) I think the documentation needs to switch to Princess and Unicorn examples instead of the Blog and Post they've been using everywhere. – Arkaine55 Sep 08 '15 at 16:32
  • 1
    thanks, i just add using System.Data.Entity; its work me. – adnan May 27 '17 at 04:15
  • team blog link is dead, [this is the new location](https://learn.microsoft.com/en-gb/archive/blogs/adonet/using-dbcontext-in-ef-4-1-part-6-loading-related-entities) – Nintynuts Jan 10 '20 at 17:52
92

Although this is implied in the question, for anyone else who has the same problem where they can't use lambdas with .Include, make sure you have this:

using System.Data.Entity;
AaronLS
  • 37,329
  • 20
  • 143
  • 202
5

No there is no official support for Include with lambda expression in RTM at the moment. I'm using this.

When we are talking about CTP4 we are meaning Entity Framework Feature. It is newer API than EF4. It mainly includes Code First and few other improvements.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Any idea when will MS support this feature ? I understand that all these extension methods has a performance issue with large / multilevel object - as they need to traverse again and again each call for include, and while the object "definition" tree is constant and can be cached, there is no solution like this yet. – Dani Dec 28 '10 at 18:09
  • Entity Framework Feature is currently in CTP5. I guess RTM version should be puplished in the first quater of 2011. So hopefully it will contain Include with lambda expression but I haven't checked its implementation yet so I can't say if it uses any kind of caching. – Ladislav Mrnka Dec 28 '10 at 18:13
  • 31
    This answer is wrong as of EF 4.3.x. The other answers are right, one must have **using System.Data.Entity** to get the overload with lamda expressions. – Eric J. May 13 '12 at 03:49
  • 1
    @EricJ: Yes. The strongly typed Include is available since EF 4.1. – Ladislav Mrnka May 13 '12 at 06:56