c# - Nhibernate 不存在的地方

标签 c# nhibernate

如何编写与该查询匹配的 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/

相关文章:

c# - ASP.NET:CreateDomain(...).CreateInstanceAndUnwrap(...) 或 Assembly.LoadFrom(...).GetExportedTypes() 抛出 FileNotFoundException

c# - 将代码从 VB 转换为 C#

c# - C# 事件参数有什么作用?

c# - NHibernate QueryOver 并访问 ="field"成员

database - 使用 NHibernate 设置正确的事务隔离模式,如何?

nhibernate - 适当映射字符串字段长度(按代码映射)

java - 如何使用 JAVA 或 C# 中的 Web 服务或 REST API 在 Microsoft Dynamics CRM 2016(版本 8)中创建案例?

c# - NHibernate 映射属性 + 脏检查

NHibernate - 将同一实体映射到同一数据库中的不同表

c# - 使用 Fluent nhibernate 自动映射继承