Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

c# - Using Include in Entity Framework 4 with lambda expressions

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...