您如何在HQL中表达以下Criteria查询?
var idArray = new int[] { 1, 2, 3, 4, 5 };
Session.CreateCriteria(typeof(Foo))
.Add(Expression.In("Id", idArray)
.List<Foo>();
我知道HQL中有一个“in”关键字,但据我所知,该关键字用于子查询,而不是像“......其中Id(1,2,3,4, 5)“或者等等。如果情况并非如此,我很乐意接受更正。
谢谢/ Erik
答案 0 :(得分:5)
试试这个:
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>();
答案 1 :(得分:1)
这也有效
ICriteria sc = session.CreateCriteria(typeof(Foo));
sc.Add(Restrictions.In("id",new[] { 1, 2 }));
siteList = sc.List();
session.Close();