Your problem lies in the fact that you're creating a new disjunction every time (in a loop). What you need to do is:
int[] ids = {1, 2, 3};
ICriterion disjunction = Restrictions.Disjunction();
foreach (int id in ids)
{
ICriterion criterion = Restrictions.Eq("Id", id)
disjunction.Add(criterion);
}
criteria.Add(disjunction);
The syntax may be a bit wrong - I'm a Hibernate guy rather than .NET :-)
To clarify, your original code would generate something like (in pseudo-code):
WHERE (OR(ID=1)) AND (OR(ID=2)) AND (OR(ID=3))
Since there's nothing to "OR" to, disjunctions were silently omitted.
Обновленный код на основе ответа ChssPly76:
using (ISession session = NHibernateHelper.OpenSession())
{
ICriteria criteria = session.CreateCriteria<TestObject>();
Junction disjunction = Restrictions.Disjunction();
int[] ids = {1, 2, 3};
foreach (int id in ids)
{
ICriterion criterion = Restrictions.Eq("Id", id);
disjunction.Add(criterion);
}
criteria.Add(disjunction);
IList<TestObject> items = criteria.List<TestObject>();
return items;
}