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;
}
I want to use it like this:
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 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!