Reputation: 6592
How can I use QueryOver's linq query separated like below ICriteria which will be used in a generic method?
ICriterion c = Restrictions.And(Restrictions.Eq("Name", "Foo"),
Restrictions.Or(Restrictions.Gt("Age", 21), Restrictions.Eq("HasCar",true)));
IQueryOver<Foo> c = session.QueryOver<Foo>().Where((k => k.Name == "Tiddles"
&& k => k.Age== 21) || k => k.Age < 21);
public static IList<T> All(ICriterion c)
{
using (var session = NHibernateHelper<T>.OpenSession())
{
var CC = session.CreateCriteria(typeof(T));
CC.Add(c);
return CC.List<T>();
}
}
Upvotes: 0
Views: 63
Reputation: 111940
The equivalent to the first line, in lambda form is
ICriterion cr = Restrictions.Where<Foo>(k => k.Name == "Foo" && (k.Age > 21 || k.HasCar));
Upvotes: 1
Reputation: 26940
You could pass in a Expression<Func<T, bool>>
which is what one of the Where
overloads accepts instead of a Criteria.
System.Linq.Expressions.Expression<Func<Foo, bool>> c = k =>
(k.Name == "Tiddles" && k.Age== 21) || k.Age < 21;
public static IList<T> All(Expression<Func<T, bool>> expression)
{
using (var session = NHibernateHelper<T>.OpenSession())
{
return session.QueryOver<T>()
.Where(expression)
.List();
}
}
Upvotes: 1