在 Entity Framework 4 中使用包含 lambda 表达式 [英] Using Include in Entity Framework 4 with lambda expressions

查看:20
本文介绍了在 Entity Framework 4 中使用包含 lambda 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多关于如何克服这个问题的文章,都与CTP4有关,或者添加我自己的扩展方法.

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

是否有一种官方"包含在 EF4 中的方式来在 include 中使用 lambda 表达式(对于第一级关系以及第二级和更高级别),还是最终没有包含在 RTM 中?

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 ?

有一个 - 我很乐意学习如何去做,因为现在在我的代码中使用 lambda 表达式(使用 #system.data.entity #system.data.linq)仍然给我:

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:

无法将 lambda 表达式转换为类型字符串",因为它不是委托类型上:

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)

推荐答案

实体框架 4.1 的 RTM 版本实际上在 EntityFramework.dll 文件中包含了扩展方法,用于通过 Include 函数预先加载 lambda.只需在您的项目中包含 DLL,您就应该能够编写如下代码:

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();

记得添加一个 Import/Using 语句来包含 System.Data.Entity 命名空间.否则编译器找不到扩展方法.例如:

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;

请参阅此 ADO.NET 团队博客文章了解更多信息.

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

这篇关于在 Entity Framework 4 中使用包含 lambda 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆