zoukankan      html  css  js  c++  java
  • NHibernate分页获取,通用条件查询方法

    解决问题:解决UI上使用NHibernate通用查询方法(仅适用于多个条件联合查询)

    第一步:在UI层建立构造条件方法:

            private IList<ICriterion> GetCondition(string customerName = "", string address = "")
            {
                Employee employee = new Employee();
                List<ICriterion> queryConditions = new List<ICriterion>();
                if (!string.IsNullOrEmpty(customerName))
                {
                    queryConditions.Add(new LikeExpression("CustomerName", customerName));
                }
                if (!string.IsNullOrEmpty(address))
                {
                    queryConditions.Add(new LikeExpression("Address", address));
                }
                return queryConditions;

            } 

    第二步:在数据访问层使用条件集合生成NHibernate查询语句:

            private IList<Employee> GetRecord(IList<ICriterion> queryConditions, int pageIndex, int pageSize, string orderField, bool isAscending)
            {
                ICriteria criteria = session.CreateCriteria(typeof(Employee));
                foreach (ICriterion cri in queryConditions)
                {
                    criteria.Add(cri);
                }
                int skipCount = (pageIndex - 1) * pageSize;
                criteria.AddOrder(new Order(orderField, isAscending));
                criteria.SetFirstResult(skipCount).SetMaxResults(pageSize);
                return criteria.List<Employee>();

            } 

  • 相关阅读:
    SQLServer 知识点
    Entity转换为ViewModel时提供的一种转换方法
    Linq中IGrouping转换为IQueryable
    封装整形属性时对应到枚举
    新的转换列表方式
    工作态度
    EasyFrame
    NewCloud
    将博客搬至CSDN
    Html的语义化
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/2305573.html
Copyright © 2011-2022 走看看