nhibernate 查询 SubQueryExpression [英] nhibernate queries SubQueryExpression

查看:40
本文介绍了nhibernate 查询 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屋!

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