Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate using QueryOver with WHERE IN

I would create a QueryOver like this

SELECT * FROM Table WHERE Field IN (1,2,3,4,5) 

I've tried with Contains method but I've encountered the Exception

"System.Exception: Unrecognised method call: System.String:Boolean Contains(System.String)"

Here my code

var qOver = _HibSession.QueryOver<MyModel>(() => baseModel)                                                                   .JoinAlias(() => baseModel.Submodels, () => subModels)   .Where(() => subModels.ID.Contains(IDsSubModels))   .List<MyModel>(); 
like image 863
Faber Avatar asked Mar 23 '11 16:03

Faber


People also ask

How do I write a query in NHibernate?

Let's have a look into a simple example of the Native SQL queries in NHibernate. The above example uses CreateSQLQuery() to get back a list of objects, and you will also notice that the root entity type you want the query to return is specified as Customer.

What is QueryOver?

QueryOver is a strongly-typed version of Criteria, and is more NHibernate specific. Pretty much anything you can do in ICriteria can be done with QueryOver.

How do you join two tables in NHibernate?

List<object[]> olist = null; olist = (_session. CreateQuery("Select pc.Id as Id, " + "pct. DescEn as DescEn,pct. DescAr as DescAr, pc.

What is projection in NHibernate?

The "projection" is kind of like plucking out what data you will need so that NHibernate only asks the database for just that data when it makes the SQL.


1 Answers

I've found the solution!! :-)

var qOver = _HibSession.QueryOver<MyModel>(() => baseModel)     .JoinAlias(() => baseModel.Submodels, () => subModels)     .WhereRestrictionOn(() => subModels.ID).IsIn(IDsSubModels)     .List<MyModel>(); 
like image 59
Faber Avatar answered Sep 17 '22 13:09

Faber