2008-12-22 11 views
19

Podczas tworzenia kryteriów dla NHibernate wszystkie kryteria są dodawane jako AND.Jak utworzyć instrukcje OR dla NHibernate?

Na przykład:

session.CreateCriteria(typeof(someobject)) 
.Add(critiera) 
.Add(other_criteria) 

następnie Wynik końcowy będzie

SELECT ... 
FROM ... 
WHERE criteria **AND** other_criteria 

chciałbym powiedzieć NHibernate dodać charakterystyczne jako "LUB"

SELECT ... 
FROM ... 
WHERE criteria **OR** other_criteria 

Każda pomoc jest docenione:

Odpowiedz

48

Poszukujesz klas Conjunction i Disjunction, które mogą być używane do łączenia różnych instrukcji w instrukcje OR i AND.

I

.Add(
    Expression.Conjunction() 
    .Add(criteria) 
    .Add(other_criteria) 
) 

LUB

.Add(
    Expression.Disjunction() 
    .Add(criteria) 
    .Add(other_criteria) 
) 
2

Można użyć Restrictions.or, tak że:

session.CreateCriteria(typeof(someobject)) 
    .Add(critiera) 
    .Add(other_criteria); 

gdzie:

other_criteria = Restrictions.or("property", "value"); 

Możesz dowiedzieć się więcej na ten temat po Criteria Interface documentation of Hibernate, która jest taka sama jak NHibernate.

13

Zastosowanie Restrictions.Disjunction()

 var re1 = Restrictions.Eq(prop1, prop_value1); 
     var re2 = Restrictions.Eq(prop2, prop_value2); 
     var re3 = Restrictions.Eq(prop3, prop_value3); 

     var or = Restrictions.Disjunction(); 
     or.Add(re1).Add(re2).Add(re3); 

     criteria.Add(or); 
Powiązane problemy