开发者

Understanding ICriteria for NHibernate

开发者 https://www.devze.com 2022-12-18 17:14 出处:网络
Please can someone explain in english what the following code does? var subCriteria = DetachedCriteria.For<开发者_如何学PythonUserLocation>();

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消