Nhibernate HQL where IN query

纵饮孤独 提交于 2019-12-18 04:39:04

问题


Im trying to return a SimpleQuery list that queries a single table and uses IN. I can get this to work using

return new List<Jobs>(
    ActiveRecordMediator<Jobs>.FindAll(Expression.In("ServiceId", ids))
);

However this is really really really slow. So id like to do something like this

SimpleQuery<Job> query = 
    new SimpleQuery<Job>(@"from Job as j where ? in (j.ServiceId)", ids);

return new List<Job>(query.Execute());

However I cant get the SimpleQuery to work. I cant find any documentation covering this and was hoping someone out there would be able to help.

Thanks


回答1:


Have a look at the NHibernate HQL documentation here.

I'm guessing from your code, that you're after a HQL query to return all jobs where the job.ServiceID in a list of ids.

Maybe something along the lines,

IQuery q = s.CreateQuery("from Job as j where j.ServiceId in (:serviceIds)");
q.SetParameterList("serviceIds", ids); 

BTW, have you heard of the NHibernate Lambda Extensions project? Below is an example of the IN query done using the the mentioned library. Might be something interesting to look at as an alternative to using HQL.

DetachedCriteria after =
    DetachedCriteria.For<Person>()
        .Add(SqlExpression.In<Person>(p => p.Name, 
          new string[] { "name1", "name2", "name3" }));


来源:https://stackoverflow.com/questions/2401950/nhibernate-hql-where-in-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!