我想使用nHibernate的QueryOver进行此查询。
我有两个实体:
Car
-id
- HasMany Models
Model
-id
我有这个查询,但它不起作用:
Session.QueryOver<Car>()
.JoinQueryOver(c => c.Models)
.WhereRestrictionOn(m => m.Select(s => s.Id)).IsIn(models.Select(s => s.Id).ToList())
.ToList<Car>();
sql看起来像(从我记忆中):
SELECT *
FROM Cars c
INNER JOIN Models m ON (c.id = m.car_id)
WHERE 1 = 0
但我一直在寻找:
SELECT c.*
FROM Cars c
INNER JOIN Models m ON (c.id = m.car_id)
WHERE m.id in (1,2,3,4,5)
我的查询有什么问题?
答案 0 :(得分:1)
NH对于流畅的m => m.Select(s => s.Id)
感到困惑。根本原因是,编译器选择了错误的JoinQueryOver重载,它返回ICollection而不是Model。使用:
Session.QueryOver<Car>()
.JoinQueryOver<Model>(c => c.Models)
.WhereRestrictionOn(m => m.Id).IsIn(models.Select(s => s.Id).ToList())
.ToList<Car>();