NHibernate ORs using QueryOver
I am trying to create a simple select with an OR clause using NHibernate 3.3 using QueryOver
. So for a simple choice of type:
Select x,y,z from myTable where x='one' or y = 'two' or z = 'three'
I came up with this:
IList<MyTable> list = session.QueryOver< MyTable >()
.WhereRestrictionOn(
Restrictions.Or(Restrictions.On< MyTable >(tab => tab.x == "One"),
Restrictions.On< MyTable >(tab => tab.y == "Two") )
);
It won't compile and TBH I suspect I am going in the wrong direction.
+3
source to share
1 answer
This syntax should solve it
var list = session.QueryOver<MyTable>()
.Where(
Restrictions.Or(
Restrictions.Eq(Projections.Property<MyTable>(tab => tab.x), "One"),
Restrictions.Eq(Projections.Property<MyTable>(tab => tab.y), "Two")
)
)
.List<MyTable>()
If we want more, Disjunction
this is our path to multiple ORs:
var list = session.QueryOver<MyTable>()
.Where(Restrictions.Disjunction()
.Add(Restrictions.Eq(Projections.Property<MyTable>(tab => tab.x), "One"))
.Add(Restrictions.Eq(Projections.Property<MyTable>(tab => tab.x), "Two"))
.Add(Restrictions.Eq(Projections.Property<MyTable>(tab => tab.x), "xxx"))
)
.List<MyTable>()
+3
source to share