2016-08-11 6 views
0

私はwhere句 http://www.andrewwhitaker.com/blog/2014/10/24/queryover-series-part-8-working-with-subqueries/ORサブクエリnhibernateで内部がヌルかどうかを確認します。

var popularProduct = session.QueryOver<Product>() 
    .WithSubquery.Where(()=>product.Id == 
     QueryOver.Of<TransactionHistory>() 
      .Select(tx => tx.Product.Id) 
      .OrderBy(tx => tx.Quantity) 
      .Desc 
      .Take(1) 
      .As<int>()) 
    .SingleOrDefault<Product>(); 

内のサブクエリを含めるために、次のクエリを使用していますこれは、これは私が必要なものである

SELECT 
    * 
FROM 
    Production.Product this_ 
WHERE 
    this_.ProductID = (
     SELECT 
      TOP (1) this_0_.ProductID as y0_ 
     FROM 
      Production.TransactionHistory this_0_ 
     ORDER BY 
      this_0_.Quantity desc 
    ); 

を生成SQLです。

SELECT 
    * 
FROM 
    Production.Product this_ 
WHERE 
    (this_.ProductID = (
     SELECT 
      TOP (1) this_0_.ProductID as y0_ 
     FROM 
      Production.TransactionHistory this_0_ 
     ORDER BY 
      this_0_.Quantity desc) OR this_.ProductID IS null) 
    ); 

var popularProduct = session.QueryOver<Product>() 
     .WithSubquery.Where(()=> product.Id == null || product.Id == 
      QueryOver.Of<TransactionHistory>() 
       .Select(tx => tx.Product.Id) 
       .OrderBy(tx => tx.Quantity) 
       .Desc 
       .Take(1) 
       .As<int>()) 
     .SingleOrDefault<Product>(); 
    It throws the following exception 
Could not determine member from (Convert(value(<>c__DisplayClass240).product.Id) == null) 

答えて

0
I solved my own problem by using Disjunction 


Disjunction disjunction = Restrictions.Disjunction(); 
       disjunction.Add(Restrictions.On(() => product.Id).IsNull); 
       disjunction.Add(Subqueries.Where(() => product.Id == QueryOver.Of<RequirementNote>().Select(a => a.Id).Where(req => req.PID == product.PID).OrderBy(p => p.Id).Desc().Take(1).As<int>())); 

それについてのリンク会談。

http://www.andrewwhitaker.com/blog/2014/10/24/queryover-series-part-8-working-with-subqueries/

関連する問題