Reputation: 654
I want to do the following in nhibernate. I am using criteria query on nhibernate. Does criteria query support the equivalent of this sql statement ?
select * from table where tableid in (1,2,3,4)
Upvotes: 1
Views: 454
Reputation: 27944
With the QueryOver interface:
session.QueryOver<MappedType>().AndRestrictionOn(m => m.tableid).IsIn(new int[] { 1, 2 , 3 , 4 }).List();
or
session.QueryOver<MappedType>().Where(m=> m.tableid.IsIn(new int[] { 1, 2 , 3 , 4 })).List();
or with the Criteria interface:
session.CreateCriteria<MappedType>().Add(Expression.In("tableId", new int[] { 1, 2, 3, 4 } ) ).List<MappedType>();
Upvotes: 3
Reputation: 5319
As simple as:
CurrentSession
.CreateCriteria( typeof(MappedType) )
.Add( Expression.In("MappedType.MappedId", new int[] { 1, 2, 3, 4 } ) );
Upvotes: 4
Reputation: 3558
Yes it does, ie:
ISession session = GetSession();
var criteria = session.CreateCriteria(typeof(Product));
var ids= new[] {1,2,3};
criteria.Add(new InExpression("Id", ids));
var products = criteria.List<Product>();
Upvotes: 3