在实体框架4中使用lambda表达式

编程入门 行业动态 更新时间:2024-10-28 16:16:30
本文介绍了在实体框架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表达式

本文发布于:2023-11-07 04:40:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565579.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   实体   框架   lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!