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.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
Daniel Williams
  • 8,912
  • 15
  • 68
  • 107
  • possible duplicate of [NHibernate using QueryOver with WHERE IN](http://stackoverflow.com/questions/5408781/nhibernate-using-queryover-with-where-in) – Stefan Steinegger Jun 19 '15 at 08:43

1 Answers1

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!

Daniel Williams
  • 8,912
  • 15
  • 68
  • 107