Erik Öjebo
Erik Öjebo

Reputation: 10851

NHibernate: HQL equivalent of Criteria Expression.In()?

How would you express the following Criteria query in HQL?

var idArray = new int[] { 1, 2, 3, 4, 5 };

Session.CreateCriteria(typeof(Foo))
    .Add(Expression.In("Id", idArray)
    .List<Foo>();

I am aware of that there is an "in" keyword in HQL, but as I understand it that keyword is for use with subqueries rather than something like "... where Id in (1, 2, 3, 4, 5)" or such. If that is not the case, I will gladly accept corrections.

Thanks /Erik

Upvotes: 1

Views: 3496

Answers (2)

Constantin
Constantin

Reputation: 2318

This works too

ICriteria sc = session.CreateCriteria(typeof(Foo));
sc.Add(Restrictions.In("id",new[] { 1, 2 }));
siteList = sc.List();
session.Close();

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038940

Try this:

var idArray = new int[] { 1, 2, 3, 4, 5 };
var foos = Session
    .CreateQuery("from Foo f where f.Id in (:ids)")
    .SetParameterList("ids", idArray)
    .List<Foo>();

Upvotes: 5

Related Questions