93

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)
2
  • 1
    no, it doesn't exist (apart from CTP4 - as you say). I use extension methods/enums to achieve the type safety. Commented Dec 28, 2010 at 9:30
  • I did that too, so MS included it on CTP4 and removed it in RTM ? Commented Dec 28, 2010 at 10:04

3 Answers 3

241

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.

Sign up to request clarification or add additional context in comments.

3 Comments

While the link to the ADO.net blog is still relevant, the EF6 reference/guide for the same topic is: Loading Related Entities I think the documentation needs to switch to Princess and Unicorn examples instead of the Blog and Post they've been using everywhere.
thanks, i just add using System.Data.Entity; its work me.
team blog link is dead, this is the new location
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;

Comments

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.

4 Comments

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.
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.
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.
@EricJ: Yes. The strongly typed Include is available since EF 4.1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.