为什么我的 IQueryable LINQtoObject 被视为 LINQtoSQL 并抛出不支持的 SQL 转换
我有一个 LINQ dbml 类,我将其包装在 POCO 中。我已经构建了采用 DBML 类和 init 的重载构造函数。基于传入的 dbml 对象的包装器对象属性。
例如
public class MyPerson{
public MyPerson(DBMLPerson p)
{
this.ID = p.ID;
this.Name = p.Name;
}
}
,如果我然后执行类似的操作,其中返回 IQueryable
{
return from p in datacontext.DBMLPerson
select new MyPerson(p){};
}
当我尝试对该 Iquearble 进行进一步查询时,我得到“System.NotSupportedException:成员 'MyPerson.ID ' 不支持对 SQL 的翻译.."
但是如果我这样做 {
return from p in datacontext.DBMLPerson
select new MyPerson(){
ID = p.ID;
Name = p.Name;
};
}
我根本没有收到错误,一切都很完美。基本上我想让我的类处理从 LINQ 对象到 POCO 本身的转换。
基本上我必须使用对象初始值设定项,否则我无法在该字段上匹配。
I have a LINQ dbml class that I am wrapping in a POCO. I have built overloaded constructors that take the DBML class and init. the wrapper objects properties based on the dbml object passed in.
For example
public class MyPerson{
public MyPerson(DBMLPerson p)
{
this.ID = p.ID;
this.Name = p.Name;
}
}
if I then do something like this where I return an IQueryable
{
return from p in datacontext.DBMLPerson
select new MyPerson(p){};
}
When I try to do further queries on that Iquearble I get "System.NotSupportedException: The member 'MyPerson.ID' has no supported translation to SQL.."
However if I do this
{
return from p in datacontext.DBMLPerson
select new MyPerson(){
ID = p.ID;
Name = p.Name;
};
}
I don't get an error at all and everything works perfect. Basically I want to have my class handle the conversion from LINQ object to POCO itself.
Basically I have to use the Object Initializer or I am unable to match on that field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,不确定这实际上对除了我自己之外的任何人都有帮助,但我的整个问题是我不应该在某个点之后使用 IQuerable(在我的存储库之外)
iqueryable-can-kill-your-dog -偷走你的妻子杀死你的生存意志等
Ok not sure this will actually help anyone but but myself but my whole problem is the I shouldn't be using IQuerable after a certain point(outside of my repository)
iqueryable-can-kill-your-dog-steal-your-wife-kill-your-will-to-live-etc