0

In my project I try to buid criteria like this:

ICriteria criteria = session.CreateCriteria(typeof(Orders), typeof(Orders).Name);

if (param != null)
{                       
    if (param[1] != "System")
    {
        for (int index = 0; index < param.Length - 1; index++)
        {
            if (!string.IsNullOrEmpty(param[index]))
            {                                
                criteria.Add(Expression.Eq(
                    "RealizationPoint", 
                    CommonUtils.GetNameRealizationPointById(param[index])));
            }
        }
    }
    if (param[1] != "System" && param2 != null && 
        !string.IsNullOrEmpty(param2[0]))
    {
        for (int index = 0; index < param2.Length - 1; index++)
        {
            if (!string.IsNullOrEmpty(param2[index]))
            {
                criteria.Add(Expression.Eq(
                    "RealizationPoint", 
                    CommonUtils.GetNameRealizationPointById(param2[index])));
            }
        }
     }
 }

para1, param2: string[] param1, string[] param2. In result between expression stay AND, I need OR.

Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47
Darien Fawkes
  • 3,023
  • 7
  • 24
  • 35

1 Answers1

1

You could use Restrictions.Disjunction() to group multiple expressions as OR and Restrictions.Conjuctions() as AND.

Simplified example:

var conjunction1 = Restrictions.Conjunction();
for (int index = 0; index < param.Length -1; index++)
{
    conjunction1.Add(Restrictions.Eq("RealizationPoint", param[index]));
}

var conjunction2 = Restrictions.Conjunction();
for (int index = 0; index < param2.Length -1; index++)
{
    conjunction2.Add(Restrictions.Eq("RealizationPoint", param2[index]));
}

criteria.Add(Restrictions.Disjunction().Add(conjunction1).Add(conjunction2));

// Or with Restrictions.Or()
// criteria.Add(Restrictions.Or(conjunction1, conjunction2));

You can find more info and alternatives in this SO question: How to create OR statements for NHibernate?

Community
  • 1
  • 1
Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47