简体   繁体   中英

Convert SQL query to NHibernate criteria query

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)

As simple as:

CurrentSession
  .CreateCriteria( typeof(MappedType) )
  .Add( Expression.In("MappedType.MappedId", new int[] { 1, 2, 3, 4 } ) );

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>();

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>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM