该成员不支持 SQL 转换。当尝试在 LINQ to SQL 语句中访问 Partial 类中的属性时。拉姆达表达式?
我的数据库中有 3 个表
- Country
- City
- House
Country 表看起来像
CountryID
Name
City 表看起来像
CountryID
CityID
Name
House
CountryID
CityID
HouseID
Name
我使用 LINQ to SQL 并且上面的表成为类并具有它们的属性等。
现在我有自己的抽象类,名为
Location
并为以下对象创建了部分类 Country、City、House 继承了 Location 抽象类
,因此可以有共同点 FindByID() 等功能和 .Parent(Location 类型)等属性
现在让我们采用 .Parent 它基本上返回每个类的 Parent
,因此
House 将返回
.Parent //as City
City 将返回
.Parent //as Country
Country 将返回
.Parent //as Country
Now 当尝试
在 LINQ to SQL 语句中使用 City.Parent 时,我得到一个
The member 'Location`1[City].Parent' has no supported translation to SQL.
Now 人们一直在提到如何使用 Lambda 表达式来解决这个问题。有人能给我一个关于这种情况的好例子吗
.Parent 应该是什么样子
Location Parent
{
get
{
//Please fill Lambda expression here
}
}
I have 3 tables in my database
- Country
- City
- House
Country table looks like
CountryID
Name
City table looks like
CountryID
CityID
Name
House
CountryID
CityID
HouseID
Name
I use LINQ to SQL and the above tables become classes and have their properties etc.
Now I have my own abstract class called
Location
And created Partial Classes for
Country, City, House that inherit the Location abstract class
and hence can have common
functionality like FindByID() and properties like .Parent (of type Location)
Now lets take .Parent
which basically returns the Parent for each class
so
House will return
.Parent //as City
City will return
.Parent //as Country
Country will return
.Parent //as Country
Now when trying to use
City.Parent in a LINQ to SQL statement I get a
The member 'Location`1[City].Parent' has no supported translation to SQL.
Now people have been mentioning about how you can use Lambda expressions to resolve this. Can someone give me a good example for this kind of situation
what should .Parent look like
Location Parent
{
get
{
//Please fill Lambda expression here
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
.Parent
属性在 LINQ-to-Objects 中没问题,但正如消息所述,如果需要创建 TSQL,它将不知道如何处理它。它无法通读您的 C# 代码(当时为 IL)来推断含义。充其量,您可以使用返回某种形式的表达式
的函数来执行某些操作,但使用起来并不有趣。基本上,当与 ORM 交互时,您只能谈论它已经知道的事情。您可以可能将 UDF 映射到数据上下文并使用它,但同样 - 不太好。
要使用
Expression
,您需要使用Expression.Invoke
和自滚动 lambda(当组合它们来执行有趣的操作时,例如将它们与预期值进行比较);这真的不会很好。我可能可以一起举一个例子,但坦率地说,我认为你的 DAL/存储库不应该使用这些额外的属性,而应该限制自己使用 ORM 属性。即使有点违反DRY。Your
.Parent
property will be fine in LINQ-to-Objects, but as the message states, it will have no clue what to do with this if it needs to create TSQL. It can't read through your C# code (IL at the time) to infer meaning. At best there may be something you can do with some function that returns anExpression
of some form, but it won't be fun to work with.Basically, when interacting with the ORM you can only talk in terms of things it already knows about. You could possibly map a UDF to the data-context and use that, but again - not pretty.
To work with
Expression
, you'd need to useExpression.Invoke
and self-rolled lambdas (when combining them to do interesting things like comparing them to an expected value); it really isn't going to be nice. I could probably get an example together, but frankly I think your DAL/repository should just not use these extra properties, but limit itself to using the ORM properties instead. Even if it violates a little DRY.