Example #1
0
        public IEnumerable <T> Get(string fieldName, object fieldValue, int baseIndex, short pageSize, out int count, string sortFieldName, SortDirection sortDirection)
        {
            IEnumerable <T> results = null;

            NHibernate.Criterion.DetachedCriteria crit = NHibernate.Criterion.DetachedCriteria.For <T>();

            //if pageSize>0 then do paging, otherwise no paging and all records will be displayed
            if (pageSize > 0)
            {
                crit.SetFirstResult(baseIndex).SetMaxResults(pageSize);
            }

            if (fieldValue == null || fieldValue == DBNull.Value)
            {
                crit.Add(Expression.IsNull(fieldName));
            }
            else
            {
                crit.Add(Expression.Eq(fieldName, fieldValue));
            }

            if (!(string.IsNullOrEmpty(sortFieldName) || string.IsNullOrWhiteSpace(sortFieldName)))
            {
                crit.AddOrder(new Order(sortFieldName, sortDirection == SortDirection.Ascending ? true : false));
            }

            results = crit.GetExecutableCriteria(this._session).List <T>();

            var rowCount = crit.SetFirstResult(0).GetExecutableCriteria(this._session).SetProjection(Projections.RowCount()).FutureValue <Int32>();

            count = rowCount.Value;

            return(results);
        }
Example #2
0
        public IEnumerable <T> Get(string fieldName, object fieldValue, int pageIndex, int pageSize, out int count, string sortFieldName, SortDirection sortDirection)
        {
            /*
             * var rowCount = _session.CreateCriteria(typeof(T)).SetProjection(Projections.RowCount()).FutureValue<Int32>();
             * IEnumerable<T> results = null;
             *
             * if (fieldValue == null || fieldValue == DBNull.Value)
             * {
             *  results = _session.CreateCriteria(typeof(T))
             *       .Add(Expression.IsNull(fieldName))
             *       .SetFirstResult(pageIndex * pageSize)
             *       .SetMaxResults(pageSize)
             *       .AddOrder(new Order(sortFieldName, sortDirection == SortDirection.Ascending ? true : false))
             *       .List<T>();
             * }
             * else
             * {
             *  results = _session.CreateCriteria(typeof(T))
             *      .Add(Expression.Eq(fieldName, fieldValue))
             *      .SetFirstResult(pageIndex * pageSize)
             *      .SetMaxResults(pageSize)
             *      .AddOrder(new Order(sortFieldName, sortDirection == SortDirection.Ascending ? true : false))
             *      .List<T>();
             * }
             *
             */

            //Rizwan:27-Jan-2012    Commented the above old code and re-wrote the code as rowCount was giving count of all rows in the table instead of those which satisfy in the criteria
            IEnumerable <T> results = null;

            NHibernate.Criterion.DetachedCriteria crit = NHibernate.Criterion.DetachedCriteria.For <T>();

            //if pageSize>0 then do paging, otherwise no paging and all records will be displayed
            if (pageSize > 0)
            {
                crit.SetFirstResult((int)pageIndex * pageSize).SetMaxResults(pageSize);
            }

            if (fieldValue == null || fieldValue == DBNull.Value)
            {
                crit.Add(Expression.IsNull(fieldName));
            }
            else
            {
                crit.Add(Expression.Eq(fieldName, fieldValue));
            }

            if (!(string.IsNullOrEmpty(sortFieldName) || string.IsNullOrWhiteSpace(sortFieldName)))
            {
                crit.AddOrder(new Order(sortFieldName, sortDirection == SortDirection.Ascending ? true : false));
            }

            results = crit.GetExecutableCriteria(this._session).List <T>();

            var rowCount = crit.SetFirstResult(0).GetExecutableCriteria(this._session).SetProjection(Projections.RowCount()).FutureValue <Int32>();

            count = rowCount.Value;

            return(results);
        }