Пример #1
0
        public PagedList <DemoArticle> GetDemoList(ArticleCriteria cr)
        {
            using (var session = sessionFactory.OpenSession())
            {
                var filter = session.CreateCriteria <Article>();
                if (cr.LastId > 0)
                {
                    filter.Add(Restrictions.Lt("Id", cr.LastId));
                }
                else
                {
                    filter.SetFirstResult(cr.StartFrom);
                }

                if (cr.UserId > 0)
                {
                    filter.Add(Restrictions.Eq("UserId", cr.UserId));
                }

                var results       = new PagedList <DemoArticle>();
                var countCreteria = (ICriteria)filter.Clone();
                results.AddRange(filter
                                 .SetProjection(Projections.ProjectionList()
                                                .Add(Projections.Id(), "Id")
                                                .Add(Projections.Property("Title"), "Title")
                                                .Add(Projections.Property("Image"), "Image")
                                                .Add(Projections.Property("ShortDescription"), "ShortDescription")
                                                .Add(Projections.Property("CreateDate"), "CreateDate")
                                                .Add(Projections.Property("LastUpdateDate"), "LastUpdateDate"))
                                 .AddOrder(Order.Desc("Id"))
                                 .SetMaxResults(cr.Count)
                                 .SetResultTransformer(Transformers.AliasToBean <DemoArticle>())
                                 .List <DemoArticle>());

                results.LinesCount = countCreteria.SetProjection(Projections.RowCount()).UniqueResult <int>();

                results.PageCount = (int)Math.Ceiling(results.LinesCount / (double)cr.Count);
                return(results);
            }
        }
Пример #2
0
        public PagedList <DemoArticle> GetArticleByTags(IEnumerable <Tag> tags, ArticleCriteria cr)
        {
            using (var session = sessionFactory.OpenSession())
            {
                ICriteria filter = session.CreateCriteria <Article>();
                if (cr.LastId > 0)
                {
                    filter.Add(Restrictions.Lt("Id", cr.LastId));
                }

                else
                {
                    filter.SetFirstResult(cr.StartFrom);
                }
                filter.CreateAlias("Tags", "tag");
                filter.Add(Restrictions.In("tag.Id", tags.Select(m => m.Id).ToArray()));

                var result        = new PagedList <DemoArticle>();
                var countCreteria = (ICriteria)filter.Clone();
                result.AddRange(filter
                                .SetProjection(Projections.Distinct(Projections.ProjectionList()
                                                                    .Add(Projections.Id(), "Id")
                                                                    .Add(Projections.Property("Title"), "Title")
                                                                    .Add(Projections.Property("Image"), "Image")
                                                                    .Add(Projections.Property("ShortDescription"), "ShortDescription")
                                                                    .Add(Projections.Property("CreateDate"), "CreateDate")
                                                                    .Add(Projections.Property("LastUpdateDate"), "LastUpdateDate")))
                                .AddOrder(Order.Desc("Id"))
                                .SetMaxResults(cr.Count)
                                .SetResultTransformer(Transformers.AliasToBean <DemoArticle>())
                                .List <DemoArticle>());
                result.LinesCount = countCreteria.SetProjection(Projections.RowCount()).UniqueResult <int>();
                result.PageCount  = (int)Math.Ceiling(result.LinesCount / (double)cr.Count);
                return(result);
            }
        }