2011-11-22 109 views
0

加入使用标准的API,我可以生成一个用于创建与上一个额外的条件加盟加盟过滤与NHibernate QueryOver

var criteria = Session.CreateCriteria<Product>() 
    .SetReadOnly(true) 
    .SetMaxResults(1) 
    .CreateAlias("ProductCategory", "U", JoinType.LeftOuterJoin, Expression.Eq("U.SubType", "Premium")) 
    .AddOrder(Order.Desc("U.Sequence")); 

这会产生一个JOIN类似这样的查询:

SELECT * FROM dbo.Product w 
LEFT JOIN dbo.ProductCategory u 
ON u.DefaultProductId = w.Id AND u.SubType = 'Premium' 

如何使用QueryOver语法做同样的事情?

回答

0

关闭我的头顶,我认为这是这样的:

ProductCategory category = null; 

var result = Session.QueryOver<Product>() 
        .JoinAlias(x => x.Categories,() => category, JoinType.LeftOuterJoin) 
        .Where(() => category.SubType == "Premium") 
        .OrderBy(() => category.Sequence).Desc 
        .Take(1) 
        .List(); 

编辑:包括排序依据,并赋予它一个考验。作品。

使用博客>文章类型例如,所生成的SQL看起来像:

SELECT this_.Id   as Id1_1_, 
     this_.Title  as Title1_1_, 
     post1_.BlogId  as BlogId3_, 
     post1_.Id   as Id3_, 
     post1_.Id   as Id3_0_, 
     post1_.Title  as Title3_0_, 
     post1_.Content  as Content3_0_, 
     post1_.DatePosted as DatePosted3_0_, 
     post1_.BlogId  as BlogId3_0_ 
FROM [Blog] this_ 
     left outer join [Post] post1_ 
     on this_.Id = post1_.BlogId 
WHERE post1_.DatePosted > '2011-11-22T19:43:11.00' /* @p0 */ 
ORDER BY post1_.DatePosted desc 
+0

感谢您的响应过载,但没有按” t似乎在JOIN上包含额外的搜索条件,这是我无法实现的那一点 –

+0

@David - 哦,对不起,我只是重新读你在做什么,我的坏。啊,我不认为这可能与QueryOver。对于需要表达式的JoinAlias/JoinQueryOver,没有重载。你可能必须坚持这个标准。 – Phill

0

.JoinAlias具有带有条款二

var result = Session.QueryOver<Product>() 
    .Left.JoinAlias(x => x.Categories,() => category, c => c.SubType == "Premium") 
    .OrderBy(() => category.Sequence).Desc 
    .Take(1) 
    .List();