-1

I know how to add OR restriction using Disjunction() but i need add to this group of criterias, and i alwasy require at least 2 criterias. Im wondering how can I add single OR to existing criterias.

Semi code example.

model.RootCriteria.Add(restriction) <- restriction one (e.g. name like 'name')

Next after few lines i want to add AND or OR restriction to already existing depending on variable.

if(AND) model.(Here add AND restriction)<- add AND to existing what is only model.RootCriteria.Add(restriction) and it works

else if(OR) model.(Here add OR restriction) <- add OR to existing what i dont know how to do this

output i want to receive is:

Where name like 'name ' (AND or OR) second restriction;

Sheryf
  • 71
  • 2
  • 13

1 Answers1

0

The way how we can work with criteria is always by .Add() opertion, which takes ICriterion

var criteria = session.CreateCriteria<Employee>();
    criteria.Add(
      Restrictions.On<Employee>((x) => x.LastName).IsLike("Xyz", MatchMode.Start)
    );

even in case we want to use OR, we have to add that via .Add(), like this:

    criteria.Add(Restrictions.Or(
        Restrictions.On<Employee>((x) => x.FirstName).IsLike("Abc", MatchMode.Start)
        , Restrictions.On<Employee>((x) => x.FirstName).IsLike("Def", MatchMode.Start)
    ));

The trick is that Restrictions.Or returns ICriterion as well...

Also check these:

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • ur example will be `where lastName like 'Xyz' AND ( firstName like 'Abc' OR firstName like 'Def')`. I just want to add OR to current critarias in statement. why theres no simple .AddOR() ir .AddAnd() function? – Sheryf Jul 31 '14 at 10:41
  • That was an example. So, solution for you: Firstly check which two statements must be joined with OR. Then, place them in `Restrictions.Or` and the result `.Add()` to criteria. NO other way... But this is working for many of us ... long time ;) Also, check my links to `Disjunction` ... if you have more statements – Radim Köhler Jul 31 '14 at 10:42