Convert SQL query to NHibernate criteria query
I want 开发者_JAVA百科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)
As simple as:
CurrentSession
.CreateCriteria( typeof(MappedType) )
.Add( Expression.In("MappedType.MappedId", new int[] { 1, 2, 3, 4 } ) );
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>();
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>();
精彩评论