为 linq to sql 实体创建可查询属性
我在 SQL Server 中有一个简单的表结构:
一个表存储自行车(Bikes)列表,第二个表存储自行车所有者(Owners)的历史列表。
我使用 Linq 2 SQL 生成了一个模型,该模型为我提供了一个“自行车”实体,该实体与“所有者”具有一对多关系,称为“历史所有者”。
到目前为止一切顺利...
现在我有很多疑问,我对当前所有者感兴趣。当前所有者是所有者表中的最新条目。
repo.Bikes().Where(b => b.HistoricalOwners.OrderByDescending(o => o.Date).First().Name == "Joe")
这可行,但有点难看,并且没有真正说出我要问的内容。最好在我的 Bike 实体上创建一个“CurrentOwner”属性并对其进行查询。
public Owner CurrentOwner
{
get
{
return HistoricalOwners.OrderByDescending(o => o.Date).First();
}
}
然后我可以这样查询:
repo.Bikes().Where(b => b.CurrentOwner.Name == "Joe")
这个可以编译,但是在运行时它会失败:
成员“CurrentOwner”没有支持对 SQL 的翻译。
这完全有道理。
有谁知道我如何能够以一种让此查询工作的方式创建 CurrentOwner ?
我做了一些研究,让 CurrentOwner 返回一个表达式树而不是一个实例,但我从来没有得到任何我真正满意的地方。
然后我想知道是否可以向 CurrentOwner 的 linq 2 sql 存储库添加额外的关系并将其限制为一对一。但我似乎无法使用 linq 2 sql 来做到这一点。我认为我可以使用 Linq 2 实体来做到这一点,但目前这不是一个选择。
有人对我如何使这项工作有任何想法吗?
预先感谢,
rmk
I have a simple table structure in SQL Server:
One table storing a list of Bikes (Bikes) and a second table storing a historical list of the bike's owners (Owners).
Using Linq 2 SQL I generated a model that gives me a 'Bike' entity with a 1 to many relationship to 'Owner' called 'HistoricalOwners'.
So far so good...
Now I have many queries where I am interested in the current owner. The current owner being the most recent entry in the owners table.
repo.Bikes().Where(b => b.HistoricalOwners.OrderByDescending(o => o.Date).First().Name == "Joe")
This works, but it is a bit ugly, and doesn't really say what I am asking. It would be nice to create a 'CurrentOwner' property on my Bike entity and query against that.
public Owner CurrentOwner
{
get
{
return HistoricalOwners.OrderByDescending(o => o.Date).First();
}
}
Then I could query like this:
repo.Bikes().Where(b => b.CurrentOwner.Name == "Joe")
This compiles, but of cources at runtime it will fail with:
The member 'CurrentOwner' has no supported translation to SQL.
Which makes total sense.
Does any one know how I might be able to create a CurrentOwner in a way that would let this query work?
I did some research with getting CurrentOwner to return an Expression tree instead of an instance, but I never got anywhere I was really happy with.
Then I wondered if I could add an extra relationship to my linq 2 sql repository for CurrentOwner and constrain it to be one to one. But I don't seem to be able to do this with linq 2 sql. I think I can do it with Linq 2 entities, but that isn't an option at this time.
Anyone got any ideas how I might make this work?
Thanks in advance,
rmk
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过:
看看这是否可以转换为 SQL?
Have you tried:
to see if that translates to SQL?
我不认为你真的可以按照你想要的方式做到这一点。
我的建议:
在
Bike
表上创建一个新的数据库列CurrentOwnerID
,它是Owner< 上的
OwnerID
的 FK /代码>表。将其映射为模型中的关联,然后您可以按照您想要的方式查询CurrentOwner
。缺点是您必须根据任何其他映射属性来管理
CurrentOwner
的值 - 您无法像原始示例中那样使用另一个查询来派生它,因此它可能不适合您的应用程序。然而,这就是我过去解决此类问题的方法,它为您的模型提供了更大的灵活性 - 您可以选择当前所有者是谁,而不是依赖于派生结果,并且它很容易理解。
I don't think you can really do this the way you want to be able to.
My suggestion:
Create a new database column,
CurrentOwnerID
, on theBike
table, which is a FK toOwnerID
on theOwner
table. Map this as an association in your model, then you can queryCurrentOwner
the way you want to.The drawback is that you have to manage the value of
CurrentOwner
as per any other mapped property - you can't derive it using another query as in your original example, so it may not be appropriate for your application.However that's the way I've solved this sort of problem in the past, and it gives more flexibility to your model - you can choose who the current owner is, rather than rely on a derived result, and it's simple to understand.