对QueryOver< T>的析取总是指根实体 [英] Disjunction on QueryOver<T> always refers to root entity

查看:81
本文介绍了对QueryOver< T>的析取总是指根实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用X个实体的析取来添加一定数量的OR条件,这些实体实现了包含日期信息的特定接口.我的问题是,当生成SQL时,所有析取条件都指向QueryOver的根实体.

I'm trying to add a certain numbers of OR conditions using disjunction on X number of entities that implements a certain interface containing date information. My problem is that when the SQL is generated all my disjunction conditions points to the root entity of my QueryOver.

我创建了一个通用方法来添加我的条件

I have created a generic method to add my conditions

    public static QueryOver<T,T2> AddChangedCondition<T,T2>(this QueryOver<T,T2> query, DateTime from, DateTime to, Disjunction disjunction) where T2 : IHaveDate
    {
        if(disjunction == null )
            disjunction = new Disjunction();

        disjunction.Add<T2>(k => (k.DeleteDate > from && k.DeleteDate  < to)
                || k.CreatedDate > from
                || k.UpdatedDate > from);

        return query;
    }

我想这样使用它:

Disjunction disjunction = null;
var query = QueryOver.Of<User>()
    .AddChangedCondition(fromDate,toDate, disjunction)
    .JoinQueryOver<Program>(user => user.Programs)
        .AddChangedCondition(fromDate,toDate, disjunction);

query.Where(disjunction);

由此产生的Sql看起来类似

Sql generated from this will look something along the lines of

select ....
from User
where
(
    (
        this_.raderadDatum > @p1 
        and this_.raderadDatum < @p2
    ) 
    or this_.skapadDatum > @p3 
    or this_.uppdateradDatum > @p4
    )
or
    (
        this_.raderadDatum > @p1 
        and this_.raderadDatum < @p2
    ) 
    or this_.skapadDatum > @p3 
    or this_.uppdateradDatum > @p4
    )

我尝试了使用别名的其他解决方案,但没有成功.希望对您有所帮助!

I've tried different solutions using aliases but no success. Would be greatful for any help!

推荐答案

以这种方式尝试,我插入了一些随机条件,并使用了别名

try in this way, I've inserted some random conditions and I used Aliases

 var qOver = _session.QueryOver<User>(() => usr)         
     .JoinAliases(() => usr.Programs, prg, JoinType.LeftOuterJoin)
     .Where(Restrictions.Or(
                Restrictions.On(() => usr.ID).IsIn(MyValue)
                ,Restrictions.Or(
                   Restrictions.On(() => prg.ID).IsIn(MyValue)
                  , Restrictions.On(() => prg.ID).IsNull)
                 )
               )
     .List<User>();

这样,您的SQL代码将如下

In this way your SQL code will be the following

SELECT ....
FROM User
INNER JOIN Program On (...conditions...)
WHERE User.ID = 'MyValue'
OR (Program.ID IN ('Value1','Value2') OR Program.ID IS NULL)

我希望这会有所帮助

这篇关于对QueryOver&lt; T&gt;的析取总是指根实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆