QueryOver IN Clause?
Asked Answered
M

1

17

I want to simulate this query:

SELECT * FROM FOO WHERE ID IN (1,2,3)

How can I do this in FNH?

var ids = new List<int>{1,2,3};
var results = session.QueryOver<Foo>().Where( x=> ids.Contains(x.id) );

But that does not work, just gives me an "unrecognized method call" exception.

Any ideas? This must be a common request.

Mcardle answered 28/8, 2012 at 8:10 Comment(1)
possible duplicate of NHibernate using QueryOver with WHERE INEupatorium
M
41

Aha- got it! The AddRestrictions has an IsIn method:

var results = session.QueryOver<Foo>().AndRestrictionOn(x=>x.id).IsIn(ids)

With this last piece we might be ready to ditch our years-old hand-rolled ORM!

Mcardle answered 28/8, 2012 at 8:15 Comment(3)
Here's a Gist with an example of the SQL generated by this.Whipple
You can also use WhereRestrictionOn instead of AndRestrictionOnMistral
what is the definition of the "ids" ?Northerly

© 2022 - 2024 — McMap. All rights reserved.