2012-09-13 4 views
0

나는 다음과 같은 기준을 buid하려고 :두 foreach에서 NHibernate 또는 조건을 작성하는 방법? 내 프로젝트에서

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 : 문자열 [] PARAM1, 문자열 [] PARAM2합니다. 표현식 체재와 결과 사이에 OR이 필요합니다.

답변

1

Restrictions.Disjunction()을 사용하여 여러 표현식을 OR로 그룹화하고 Restrictions.Conjuctions()을 AND로 그룹화 할 수 있습니다.

간단한 예 : How to create OR statements for NHibernate?

:이 SO 질문에 더 많은 정보와 대안을 찾을 수 있습니다

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));