【问题标题】:Convert SQL query to NHibernate criteria query将 SQL 查询转换为 NHibernate 条件查询
【发布时间】:2011-08-29 14:25:57
【问题描述】:

我想在 nhibernate 中执行以下操作。我在 nhibernate 上使用条件查询。条件查询是否支持此 sql 语句的等效项?

select * from table where tableid in (1,2,3,4)

【问题讨论】:

    标签: c# sql .net nhibernate


    【解决方案1】:

    就这么简单:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        是的,即:

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

        【讨论】:

          猜你喜欢
          • 2020-11-12
          • 1970-01-01
          • 2021-05-19
          • 2017-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-12
          相关资源
          最近更新 更多