[英]Convert SQL query to NHibernate criteria query
我想在nhibernate中執行以下操作。 我在nhibernate上使用條件查詢。 條件查詢是否支持此sql語句的等效項?
select * from table where tableid in (1,2,3,4)
簡單如:
CurrentSession
.CreateCriteria( typeof(MappedType) )
.Add( Expression.In("MappedType.MappedId", new int[] { 1, 2, 3, 4 } ) );
是的,即:
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>();
使用QueryOver接口:
session.QueryOver<MappedType>().AndRestrictionOn(m => m.tableid).IsIn(new int[] { 1, 2 , 3 , 4 }).List();
要么
session.QueryOver<MappedType>().Where(m=> m.tableid.IsIn(new int[] { 1, 2 , 3 , 4 })).List();
或使用Criteria接口:
session.CreateCriteria<MappedType>().Add(Expression.In("tableId", new int[] { 1, 2, 3, 4 } ) ).List<MappedType>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.