Linq-to-SQL EntitySet是不可查询的-有任何解决方法吗? [英] Linq-to-SQL EntitySet Is Not IQueryable -- Any Workarounds?

查看:45
本文介绍了Linq-to-SQL EntitySet是不可查询的-有任何解决方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在Linq-to-SQL中查询模型对象上的EntitySet属性时,它将返回该实体集中的所有行并进行进一步的客户端查询.

When you query an EntitySet property on a model object in Linq-to-SQL, it returns all rows from the entityset and does any further querying client-side.

在网上的一些地方已证实了这一点,我本人也观察到了这种行为. EntitySet没有实现IQueryable.

This is confirmed in a few places online and I've observed the behavior myself. The EntitySet does not implement IQueryable.

我要做的是转换代码,例如:

What I've had to do is convert code like:

var myChild = ... ;
// Where clause performed client-side.
var query = myChild.Parents().Where(...) ;  

收件人:

var myChild = ... ;
// Where clause performed in DB and only minimal set of rows returned.
var query = MyDataContext.Parents().Where(p => p.Child() == myChild) ;  

有人知道更好的解决方案吗?

Does anyone know a better solution?

第二个问题:这个问题在实体框架中得到了解决吗?

A secondary question: is this fixed in the Entity Framework?

推荐答案

EntitySet只是实体的集合.它实现IEnumerable,而不是IQueryable.活动记录模式指定实体直接负责其自身的持久性. OR映射器实体对持久层没有任何直接了解.或映射器将此职责以及工作单元和身份映射职责放置到数据上下文中.因此,如果需要查询数据源,则必须使用上下文(或Table对象).要更改此设置,将会使使用中的图案弯曲.

An EntitySet is just a collection of entities. It implements IEnumerable, not IQueryable. The Active Record pattern specifies that entities be directly responsible for their own persistence. OR mapper entities don't have any direct knowledge of the persistence layer. OR Mappers place this responsibility, along with Unit Of Work, and Identity Map responsibilities into the Data Context. So if you need to query the data source, you gotta use the context (or a Table object). To change this would bend the patterns in use.

这篇关于Linq-to-SQL EntitySet是不可查询的-有任何解决方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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