nhibernate queries SubQueryExpression

独自空忆成欢 提交于 2019-12-24 00:48:29

问题


Can someone explain me what are NHibernate SubQueryExpression based queries. Any links with concrete examples are very welcome.

Thanks

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.

I do not want to access like Images.First() cause that will initialize all collection, if you need more info, plase ask.


回答1:


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))) 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.

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.



来源:https://stackoverflow.com/questions/10862654/nhibernate-queries-subqueryexpression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!