nhibernate查询SubQueryExpression [英] nhibernate queries SubQueryExpression
问题描述
有人可以向我解释什么是基于NHibernate SubQueryExpression的查询.任何带有具体示例的链接都非常受欢迎.
Can someone explain me what are NHibernate SubQueryExpression based queries. Any links with concrete examples are very welcome.
谢谢
更新: 假设我有一个名为Beach的实体.那个海滩可以有很多图像.我想从图片"集合中选择海滩"实体及其第一张图片.我只想携带所选的图像对象,或者只选择第二个对象携带该对象.
Update: Let's say that I have one entity named Beach. That beach can have many images. I want to select Beach entity and it;s first image from Images collection. I want to carry arround only that selected image object, or if I select only second object to carry only that object.
我不想访问Images.First()原因,因为它将初始化所有集合,如果您需要更多信息,请询问.
I do not want to access like Images.First() cause that will initialize all collection, if you need more info, plase ask.
推荐答案
var query = session.QueryOver(() => vehicleAlias)
.Left.JoinAlias(() => vehicleAlias.VehicleRegistrations, () => vehicleRegistrationAlias)
.WithSubquery.WhereProperty(() => vehicleRegistrationAlias.RegistrationExpiryDate).Eq(
QueryOver.Of(() => vehicleRegistrationAlias2)
.Where(() => vehicleRegistrationAlias2.Vehicle.Id == vehicleAlias.Id)
.Select(Projections.Max<VehicleRegistration>(ps => ps.RegistrationExpiryDate)));
query.Left.JoinAlias(() => vehicleRegistrationAlias.VehicleRegistrants, () => vehicleRegistrantAlias)
.Where(() => vehicleRegistrantAlias.Client.Id == clientId);
这是我刚刚为工作写的子查询,花了我一段时间才写.我真的不知道您要问的具体内容,但这是一个示例.如果您有任何疑问,请告诉我.
This is a subquery I just wrote for my work that took me a while to write. I don't really know what you are asking in specific but here is a sample. If you have any questions on it let me know.
.Select(Projections.Max(ps => ps.RegistrationExpiryDate)))此行完成子查询中的所有工作.它选择最近的车辆注册.车辆登记别名2是作为子查询被查询的对象.
.Select(Projections.Max(ps => ps.RegistrationExpiryDate))) This line does all the work in the sub query. It selects the most recent vehicle registration. Vehicle registration alias 2 is the object being queried as a sub query.
因此,这将仅撤回当前车辆的车辆登记.一辆车辆可能有许多车辆登记.它的.Select语句可以修改为.OrderById.Desc.SelectTop(1)之类的东西.
So this will pull back only the current vehicle registration for a vehicle. One vehicle may have many vehicle registrations. Its the .Select statement that can be modified into something like .OrderById.Desc.SelectTop(1) or something like that.
希望此编辑对您有所帮助.
I hope this edit helps.
这篇关于nhibernate查询SubQueryExpression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!