Search code examples
c#nhibernatequeryover

Add two sub queries to the query over with Or operator instead of And?


I tried this

incidentSubquery.WithSubquery.WhereProperty(x => x.Id).In (witnessTypesSubquery).
                 WithSubquery.WhereProperty(x => x.Id).NotIn(witnessTypesSubquery);

but the operator between the two sub queries is And operator how can I make it or operator instead of and .


Solution

  • One way could be:

    incidentSubquery.Where
    (
      Restrictions.Disjunction()
        .Add(Subqueries.WhereProperty<MyEntity>(x => x.Id).In(witnessTypesSubquery))
        .Add(Subqueries.WhereProperty<MyEntity>(x => x.Id).NotIn(witnessTypesSubquery))
    );
    

    We can use Restrictions.Disjunction().Add(...).Add(...) to join as much as possible OR statements.

    Simplified version could be with Restrictions.Or(A, B) (just two statements)