Understanding ICriteria for NHibernate
Please can someone explain in english what the following code does?
var subCriteria = DetachedCriteria.For<开发者_如何学PythonUserLocation>();
subCriteria.SetProjection(Projections.Property("LocationId"))
.Add(Restrictions.Eq("UserId", userId));
return UoW.Session.CreateCriteria(typeof(Location))
.Add(Subqueries.PropertyIn("LocationId", subCriteria)).List<Location>();
Can you run the code and look at the SQL generated by it?
I'm guessing it's something pretty close to this:
SELECT *
FROM Location
WHERE LocationId IN (SELECT LocationId FROM UserLocation WHERE UserId = @UserId)
It looks like it's trying to find all the locations for a given user based on their UserId.
Subqueries.PropertyIn is running an "inner select".
SetProjection returns a subset of the possible columns.
Restrictions is used in building the WHERE clause.
DetachedCriteria and CreateCriteria are used to build up SELECT statements.
The List at the end runs the query and returns a list of objects of the given type.
精彩评论