2014-10-09 60 views
2

比較整數值我得子查詢這兩個選擇一個整數值:QueryOver:從子查詢

QueryOver<Type> sq1 = QueryOver.Of<Type>().Where(someCondition) 
              .Select(x => x.IntegerValue); 
QueryOver<Type> sq2 = QueryOver.Of<Type>().Where(somethingElse) 
              .Select(x => x.IntegerValue); 

,並希望比較其結果在我的主查詢:

mainQuery.Where(Restrictions.Disjunction().Add(Subqueries.WhereValue(sq1).Le(sq2)); 

加入了此限制後調用mainQuery.List會導致一個錯誤,它告訴我某些屬性沒有實現IConvertible,因此它看起來好像不會將sq1和sq2識別爲int值:

「參數值不能從QueryOver`2轉換成的Int32(或類似水木清華)」如何實現這一

回答

3

的想法/草案應該是這樣的:

var sq1 = QueryOver.Of<Type>()... // we must be sure that only 1 ROW is returned 
var sq2 = QueryOver.Of<Type>()... // because it will be treated as a value 

// let's create IProjection 
var left = Projections.SubQuery(sq1.DetachedCriteria); 
var right = Projections.SubQuery(sq2.DetachedCriteria); 

// the Restriction on top of two projections (we can use SimpleCriteria, but... 
// but this Expression can work with two projections... while named LeProperty 
var restriction = Expression.LeProperty(left, right); 

和這可以傳遞到主查詢:

mainQuery.Where(restriction); 

注:如果我們使用子查詢與<=>= ...他們必須準確地返回一行一列...