加入我找到了幾個網上資源,但還沒有真正能夠這樣一個梳理NHibernate的QueryOver左外與條件
基本上我有它有兩個左outter連接的查詢就可以了
var query = session.QueryOver<NewsPost>(() => newsPostAlias)
.Left.JoinQueryOver(x => newsPostAlias.PostedBy,() => userAlias)
.Left.JoinQueryOver(x => newsPostAlias.Category,() => categoryAlias)
.Fetch(x => x.PostedBy).Eager
.Fetch(x => x.Category).Eager
.Where(x => !x.Deleted);
這可能是一種無效的方式,但似乎沒有中斷。現在我想要做的是在兩個已經離開outter連接的表上,我想確保這兩個表中的Deleted列都是假的。
然而,每當我補充一點,限制居住在新聞崗位外鍵列時,結果只返回,但由於這是可空的,爲什麼我做到了左outter加入這個心不是可取的。
請告訴我基本上使其
.Where(x => !x.Deleted && !x.PostedBy.Deleted && !x.Category.Deleted);
我看着multiqueries,期貨和析取的最好方式,我不知道應該採取什麼辦法,很明顯,我能想到的幾種方法(我的直覺告訴我的壞方法)做這件事,但最新的方法是什麼? :)
感謝
編輯 - 接受的答案修改
return session.QueryOver(() => newsPostAlias)
.Fetch(x => x.PostedBy).Eager
.Fetch(x => x.Category).Eager
.Left.JoinQueryOver(() => newsPostAlias.PostedBy,() => postedByAlias)
.Left.JoinQueryOver(() => newsPostAlias.Category,() => categoryAlias)
.Where(() => !newsPostAlias.Deleted)
.And(() => newsPostAlias.PostedBy == null || !postedByAlias.Deleted)
.And(() => newsPostAlias.Category == null || !categoryAlias.Deleted)
.OrderBy(() => newsPostAlias.PostedDate).Desc
.Take(10)
.List();
對不起,但通過使用。並且我確保只有已填充的PostsBy和類別的行被返回,我不能讓這個以任何其他方式工作 – Lee 2011-05-30 16:05:33
糾正,我可以做到這一點這樣做。凡(()=>!newsPostAlias.Deleted) 。凡(()=> newsPostAlias.PostedBy == NULL ||(userAlias.Id ==用戶id &&!userAlias.Deleted)) 。凡( ()=> newsPostAlias.Category == null ||(categoryAlias.Id == categoryId &&!categoryAlias.Deleted)) – Lee 2011-05-30 16:08:51