繁体   English   中英

将SQL查询转换为NHibernate条件查询

[英]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.

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