开发者

NHibernate QueryOver: Disjuction with restriction in join

I would like to OR-combine two restrictions, one related to the parent, one related to the child.

I have the following code:

var query = Session.QueryOver<Project>()
            .Where(x => x.AccountId == Scope.AccountId)
            .And(x => x.Archived != active)
            .AndRestrictionOn(x => x.Name).IsLike("%" + text + "%")
            .JoinQueryOver(x => x.Customer)
                .WhereR开发者_如何转开发estrictionOn(c => c.Name).IsLike("%" + text + "%")
            .OrderBy(x => x.Id).Desc
            .Fetch(x => x.Customer).Eager;

SQL-output:

SELECT   this_.Id              as Id2_1_,
         this_.Account_id      as Account2_2_1_,
         this_.Name            as Name2_1_,
         this_.Info            as Info2_1_,
         this_.Archived        as Archived2_1_,
         this_.Customer_id     as Customer6_2_1_,
         customer1_.Id         as Id1_0_,
         customer1_.Account_id as Account2_1_0_,
         customer1_.Name       as Name1_0_
FROM     [Project] this_
         inner join [Customer] customer1_
           on this_.Customer_id = customer1_.Id
WHERE    this_.Account_id = 5 /* @p0 */
         and not (this_.Archived = 1 /* @p1 */)
         and this_.Name like '%dim%' /* @p2 */
         and customer1_.Name like '%dim%' /* @p3 */
ORDER BY customer1_.Id desc

Unfortunately, there is a AND-conjuction between the two restrictions:

this_.Name like '%dim%' and customer1_.Name like '%dim%'

I would like to have OR instead, like so:

this_.Name like '%dim%' or customer1_.Name like '%dim%'

How can I solve this query with the new QueryOver API?


You'll need to use aliases.

The complete query should be:

Customer customerAlias = null;

/* 1st - create base query */
var query = Session.QueryOver<Project>()
            .JoinAlias(x => x.Customer, () => customerAlias)
            .Where(x => x.AccountId == Scope.AccountId)
            .Where(x => x.Archived != active);

/* 2nd - create the disjunction */
var disjunction = new Disjunction();

disjunction.Add(Restrictions.On<Project>(p => p.Name).IsLike(text, MatchMode.Anywhere));
disjunction.Add(Restrictions.On(() => customerAlias.Name).IsLike(text, MatchMode.Anywhere));

/* 3rd - complete query */
query = query.Where(disjunction)                
             .OrderBy(x => x.Id).Desc;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜