예제 #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(typeof(Article));
                if (cr.LastId > 0)
                {
                    filter.Add(Restrictions.Lt("Id", cr.LastId));
                }

                else
                {
                    filter.SetFirstResult(cr.StartFrom);
                }
                filter.CreateAlias("Tags", "tag");
                filter.Add(Expression.In("tag.Id", tags.Select(m => m.Id).ToArray()));
                //criteria.SetProjection(Projections.Distinct(Projections.Property("Id")));

                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"))
                    .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);
            }
        }