2011-11-22 199 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();