如何编写与该查询匹配的 NHibernate 标准?
select * from InventoryItems i
where not exists (select inventory_id from InventoryItemCategories c where i.id = c.inventory_id and c.Category_Id = '805cec1e-1d7b-4062-9427-a26d010f4fb3')
我已经写了这个,但不存在只需要一个参数!
DetachedCriteria detachedCriteria =
DetachedCriteria.For(typeof (InventoryItemCategories))
.SetProjection(Projections.Property("Catgory_Id"));
var criteria=Session.CreateCriteria(typeof(InventoryItem)).
Add(Subqueries.NotExists(inventoryCategoryId,detachedCriteria));
非常感谢您提前提供的帮助
最佳答案
我们可以使用别名、昵称来获取子查询范围内的外表。因此,如果我们(稍后)将条件命名为 “myAlias”
,我们可以这样扩展 subQuery 定义:
var detachedCriteria = DetachedCriteria.For<InventoryItemCategories>()
// here we have to use the C# property names instead of the column names
.Add(Restrictions.EqProperty("InventoryId" // local table c, col inventory_id
, "myAlias.ID")) // outer table i, col id
.Add(Restrictions.Eq("CategoryId" // local table c, col Category_Id
, myGuid)) // the parameter passed alá '805ce...
.SetProjection(Projections.Property("Catgory_Id"));
调整后的标准:
// outer table nickname "myAlias" here
var criteria = session.CreateCriteria<InventoryItem>("myAlias");
criteria.Add(Subqueries.NotExists(detachedCriteria ));
关于c# - Nhibernate 不存在的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19848937/