views:

66

answers:

1

Please can someone explain in english what the following code does?

var subCriteria = DetachedCriteria.For<UserLocation>();

subCriteria.SetProjection(Projections.Property("LocationId"))
           .Add(Restrictions.Eq("UserId", userId));

return UoW.Session.CreateCriteria(typeof(Location))
       .Add(Subqueries.PropertyIn("LocationId", subCriteria)).List<Location>();
+2  A: 

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.

Michael Maddox