Implementing “where not exists” with NHibernate QueryOver

后端 未结 2 1988
我在风中等你
我在风中等你 2021-01-15 00:10

Using the new QueryOver API in NHibernate, I need to do something equivalent of:

select c.*
from Category c
where not exists (
    select *
             


        
相关标签:
2条回答
  • 2021-01-15 00:32

    Not tested, but something like this

    IEnumerable<Category> FindCategoriesWithoutProduct(Product product)
    {
        CategoryProduct categoryProductAlias = null;
        var subQuery = QueryOver.Of<CategoryProduct>(() => categoryProductAlias)   
            .Select(x => categryProductAlias.ID)
            .Where(() => categoryProductAlias.ProductID == "DogFood");
    
        Category categoryAlias = null;
        return _session
            .QueryOver<Category>(() => categoryAlias)
            .WithSubquery.WhereProperty(() => clientAlias.Id).NotIn(subQuery)
            .List();
    }
    
    0 讨论(0)
  • 2021-01-15 00:49

    I'm just running in the same problem, and possible solution is:

    Category aliasCategory = null;
    CategoryProduct aliasCategoryProduct = null;
    
    var qcp = QueryOver.Of<CategoryProduct>(() => aliasCategoryProduct)
              .Where(() => aliasCategoryProduct.ProductID == "DogFood")
              .Where(() => aliasCategory.Id == aliasCategoryProduct.CategoryID)
              .DetachedCriteria;
    
    return _session.QueryOver<Category>(() => aliasCategory)
                   .Where(Subqueries.NotExists(qcp));
    

    It works on my similar criteria.

    0 讨论(0)
提交回复
热议问题