Beispiel #1
0
        public IPagedList <Album> GetAlbums(ILoadOptions options)
        {
            IPagedList <Album> result = new PagedList <Album>();

            if (options == null)
            {
                return(result);
            }

            if (options.MaxResults <= 0)
            {
                return(result);
            }

            ISession session = SessionFactory.GetSession();

            try
            {
                DetachedCriteria countCriteria = GetAlbumsImpl(options);
                DetachedCriteria listCriteria  = GetAlbumsImpl(options);

                countCriteria.SetProjection(Projections.RowCount());
                countCriteria.ClearOrders();

                listCriteria.
                SetFirstResult(options.FirstResult).
                SetMaxResults(options.MaxResults);

                IMultiCriteria multiCriteria = session.CreateMultiCriteria();
                multiCriteria.Add(countCriteria);
                multiCriteria.Add(listCriteria);


                IList queryResult = multiCriteria.List();

                result.TotalItems = (int)((IList)queryResult[0])[0];

                IList recordsList = (IList)queryResult[1];

                EntityConverter entityConverter = new EntityConverter();

                foreach (var e in recordsList)
                {
                    AlbumEntity dataEntity     = e as AlbumEntity;
                    Album       businessEntity = entityConverter.FromDataEntity(dataEntity, AlbumConvertOptions.Small);
                    result.Add(businessEntity);
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
            }
            finally
            {
                session.Close();
            }

            return(result);
        }
Beispiel #2
0
        private static DetachedCriteria TransformToRowCount(DetachedCriteria criteria)
        {
            DetachedCriteria cloned = CriteriaTransformer.Clone(criteria);

            cloned.ClearOrders();
            cloned.SetResultTransformer(null).SetFirstResult(0).SetMaxResults(RowSelection.NoValue).SetProjection(
                Projections.RowCountInt64());
            return(cloned);
        }
        /// <summary>
        /// 動態查詢筆數
        /// </summary>
        /// <returns>總筆數</returns>
        public int CountByDetachedCriteria(DetachedCriteria detachedcriteria)
        {
            ISession session = HibernateTemplate.SessionFactory.GetCurrentSession();
            //ISession session = NHibernateHelper.GetCurrentSession();
            DetachedCriteria tmpCriteria = CriteriaTransformer.Clone(detachedcriteria);

            tmpCriteria.ClearOrders();

            ICriteria criteria = tmpCriteria.GetExecutableCriteria(session);

            ProjectionList projection = Projections.ProjectionList();

            projection.Add(Projections.RowCount());
            return((int)criteria.SetProjection(projection).UniqueResult());
        }
 public void ClearOrders()
 {
     detachedCriteria.ClearOrders();
 }