0

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>();
        }
    }
2
  • 3
    Can you be a little more clear about what you're trying to accomplish? Commented May 21, 2015 at 16:48
  • I am trying to give query as variable to a method Commented May 21, 2015 at 17:04

2 Answers 2

1

The equivalent to the first line, in lambda form is

ICriterion cr = Restrictions.Where<Foo>(k => k.Name == "Foo" && (k.Age > 21 || k.HasCar));
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.