QueryOver:比较子查询中的整数值

时间:2014-10-09 10:36:35

标签: .net nhibernate 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(或类似于smth)”

1 个答案:

答案 0 :(得分: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);

注意:如果我们使用带有<=>=的子查询...则必须返回一行和一列... < / p>