如何在Join语句中指定多个条件,而不是在NHibernate QueryOver中的Where块中指定

时间:2015-02-08 04:31:39

标签: mysql asp.net-mvc-3 nhibernate queryover

以下QueryOver在Where块中生成子查询,但我正在寻找在连接语句中指定该条件的方法

var productsWithLatestComments = _sessionHelper.GetSession().QueryOver(() => p)
                .Left.JoinAlias(() => p.Comments, () => cm)
                .WithSubquery.Where(() => cm.CommentDate == QueryOver.Of<Comment>()
                    .Where(c => c.Product.Id == p.Id)
                    .SelectList(list => list.SelectMax(c => c.CommentDate)).As<DateTime>())
                .Where(() => p.Status != "NOT SOLD" )
                .SelectList(list => list ....GET THE LIST OF COLS.........

这类似

SELECT this_.id as y0_, ......... FROM product this_ 
left outer join comment cn1_ on this_.id=cn1_.product_id 
WHERE cn1_.comment_date = (SELECT max(this_0_.created_date) as y0_ FROM comment this_0_ WHERE this_0_.product_id = this_.id) and (not (this_.status = ?p0);?p0 = 'Sold Out'' [Type: String (18)]

但我一直在寻找

SELECT this_.id as y0_, ......... FROM product this_ 
left outer join comment cn1_ on this_.id=cn1_.product_id and cn1_.comment_date = (SELECT max(this_0_.created_date) as y0_ FROM comment this_0_ WHERE this_0_.product_id = this_.id)
WHERE (not (this_.status = ?p0);?p0 = 'Sold Out'' [Type: String (18)]

1 个答案:

答案 0 :(得分:0)

好的,我通过在连接别名中指定子查询而不是在内部.WithSubquery

,在QueryOver中进行了一些更改。
var productsWithLatestComments = _sessionHelper.GetSession().QueryOver(() => p)
                .JoinAlias(() => p.Comments, () => cm, JoinType.LeftOuterJoin, Subqueries.Where(() => cm.CommentDate == QueryOver.Of<Comment>()
                    .Where(c => c.Product.Id == p.Id)
                    .SelectList(list => list.SelectMax(c => c.CommentDate)).As<DateTime>()))
                .Where(() => p.Status != "NOT SOLD" )
                .SelectList(list => list ....GET THE LIST OF COLS.........