了解 NHibernate 的 ICriteria
请有人用英语解释一下以下代码的作用吗?
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>();
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>();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能运行代码并查看它生成的 SQL 吗?
我猜这与此非常接近:
看起来它试图根据给定用户的 UserId 查找其所有位置。
Subqueries.PropertyIn 正在运行“内部选择”。
SetProjection 返回可能列的子集。
限制用于构建 WHERE 子句。
DetachedCriteria 和 CreateCriteria 用于构建 SELECT 语句。
最后的 List 运行查询并返回给定类型的对象列表。
Can you run the code and look at the SQL generated by it?
I'm guessing it's something pretty close to this:
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.