views:

806

answers:

1

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

+2  A: 

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" }));
Noel
Thanks for the reply.I edited my query based on your replySimpleQuery<Job> query = new SimpleQuery<Job>( @"from Job as j where j.SecondName = (:name) and ServiceId in (:serviceIds)"); query.SetParameter("serviceIds", ids); query.SetParameter("name", name); return new List<Job>(query.Execute());The only issue now is the int array of ids i'm passing in. The field in the database is type int, if i use IN on any of the string fields it works fine and if i pass a single int value it works. How can I pass an array? Thanks
Gilbert
Opps, wrong method callTry using q.SetParameterList instead
Noel
Also this might be useful to you as wellhttp://www.nhforge.org/doc/nh/en/index.html#manipulatingdata-queryinterface
Noel
Excellent, that did the trick. Thank you very much :-)
Gilbert