问题
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