在实体框架4中使用lambda表达式 [英] Using Include in Entity Framework 4 with lambda expressions

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

问题描述

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



是否有官方EF4包含使用lambda表达式的方式(包括第一级关系以及第二级和更多级别),还是最终都不包括在RTM中?有一个 - 我会很高兴学习如何做到这一点,就像在现在的代码中使用lambda表达式(用#system.data.entity #system)一样。 data.linq)仍然给我:



无法将lambda表达式转换为类型'string',因为它不是代理类型
on:

  var customers = from c in 
context.Customers.Include(c => c.Phone)


解决方案

实体框架4.1的RTM版本实际上包括> code> EntityFramework.dll 文件中的扩展方法,以便通过 Include 函数加载lambda。只需将DLL包含在你的项目中,你就可以编写如下代码:

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

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

 使用System.Data.Entity; 

看到这个 ADO.NET团队博客文章了解更多信息。


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)

解决方案

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.

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

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