我在网上找到了一些资源,但实际上并没有能够解决这个问题
基本上我有一个查询,其上有两个左外连接
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);
这可能是一种无效的方式,但似乎没有中断。现在我要做的是在两个已经离开了连接的表上,我想确保这两个表中的Deleted列都是假的。
但是每当我添加该限制时,结果只会在填充新闻帖子中的外键列时返回,但由于这是可以为空的,为什么我将其作为左外连接,这是不可取的。
什么是基本上最好的方式
.Where(x => !x.Deleted && !x.PostedBy.Deleted && !x.Category.Deleted);
我研究了多种查询,期货和分离,我不确定应该采取什么方法,显然我可以想到一些方法(我的直觉告诉我的方法)这样做但是方法是什么? :)
由于
编辑 - 接受的答案修改
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();
答案 0 :(得分:7)
我想你的查询应该是这样的
Session.QueryOver<NewsPost>()
.Left.JoinAlias(x => x.PostedBy, () => userAlias)
.Left.JoinAlias(x => x.Category, () => categoryAlias)
.Where(x => !x.Deleted)
.And(x => !userAlias.Deleted)
.And(x => !categoryAlias.Deleted);
答案 1 :(得分:0)
这似乎有用......
var posts = session.QueryOver<NewsPost>()
.Left.JoinAlias(x => x.Category, () => category)
.Left.JoinAlias(x => x.PostedBy, () => user)
.Where(x => x.Deleted == false)
.Where(Restrictions
.Or(
Restrictions.Where(() => user.Deleted == false),
Restrictions.Where<NewsPost>(x => x.PostedBy == null)
)
)
.Where(Restrictions
.Or(
Restrictions.Where(() => category.Deleted == false),
Restrictions.Where<NewsPost>(x => x.Category == null)
)
)
.List();
这种感觉会不会很糟糕?如果是这样,你能解释一下原因吗?我不太了解优化sql,因此我在问......