/// <summary>
        /// 把<see cref="IOrderedQueryable{T}"/>集合继续按指定字段排序方式进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="propertyName">排序属性名</param>
        /// <param name="sortDirection">排序方向</param>
        /// <returns></returns>
        public static IOrderedEnumerable <T> ThenBy <T>(this IOrderedEnumerable <T> source,
                                                        string propertyName,
                                                        ListSortDirection sortDirection = ListSortDirection.Ascending)
        {
            propertyName.CheckNotNullOrEmpty("propertyName");

            return(CollectionPropertySorter <T> .ThenBy(source, propertyName, sortDirection));
        }
        public override List <T> GetList <T>(Expression <Func <EventProfileEntity, bool> > predicate, PageCondition con)
        {
            int total     = con.RowCount;
            int pageIndex = con.PageIndex;
            int pageSize  = con.PageSize;
            List <SortCondition> sortConditions = con.SortConditions;

            //if (total <= 0)
            //{
            //    total = Repository.Entities.Count(predicate);
            //}
            var source = Repository.Entities.Distinct().Where(predicate);

            if (sortConditions == null || sortConditions.Count == 0)
            {
                source = source.OrderByDescending(m => m.Id);
            }
            else
            {
                int count = 0;
                IOrderedQueryable <EventProfileEntity> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <EventProfileEntity> .OrderBy(source, sortCondition.SortField,
                                                                                 sortCondition.ListSortDirection)
                        : CollectionPropertySorter <EventProfileEntity> .ThenBy(orderSource, sortCondition.SortField,
                                                                                sortCondition.ListSortDirection);

                    count++;
                }
                source = orderSource;
            }

            //var lst = source != null
            //    ? source.Skip((pageIndex - 1) * pageSize).Take(pageSize)
            //    : Enumerable.Empty<EventProfileEntity>();

            IEnumerable <EventProfileEntity> resultList = null;

            if (source != null)
            {
                resultList = source.ToList().Distinct(new EventProfileComparer());
                total      = resultList.ToList().Count;
                resultList = resultList.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            }
            else
            {
                resultList = Enumerable.Empty <EventProfileEntity>();
                total      = 0;
            }

            con.RowCount = total;

            return(resultList.ToList().Select(n => (T)(new T().ConvertAPIModel(n))).ToList());
        }
Esempio n. 3
0
        /// <summary>
        /// 从指定<see cref="IQueryable{T}"/>集合中查询指定数据筛选的分页信息返回带List<Hashtable>集合
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <typeparam name="TResult">分页数据类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="sortConditions">排序条件集合</param>
        /// <param name="selector">数据筛选表达式</param>
        /// <returns>分页结果信息</returns>

        public static PageResultHashtable TopageHashTable <TEntity, TResult>(this IQueryable <TEntity> source, Expression <Func <TEntity, bool> > predicate,
                                                                             int pageIndex,
                                                                             int pageSize,
                                                                             SortCondition[] sortConditions,
                                                                             Expression <Func <TEntity, TResult> > selector)
        {
            int total = source.Count(predicate);

            source = source.Where(predicate);
            if (sortConditions == null || sortConditions.Length == 0)
            {
                source = source.OrderBy("Id");
            }
            else
            {
                int count = 0;
                IOrderedQueryable <TEntity> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <TEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }
                source = orderSource;
            }
            PageResultHashtable ph     = new PageResultHashtable();
            List <Hashtable>    listht = new List <Hashtable>();

            ph.Total = total;
            if (source != null)
            {
                var list = source.Skip((pageIndex - 1) * pageSize).Take(pageSize).Select(selector).ToList();
                if (list != null && list.Count > 0)
                {
                    Type           t   = list.FirstOrDefault().GetType();
                    PropertyInfo[] pis = t.GetProperties();
                    foreach (var item in list)
                    {
                        Hashtable ht = new Hashtable();
                        foreach (var pi in pis)
                        {
                            object val = pi.GetValue(item, null);
                            ht.Add(pi.Name, val);
                        }
                        listht.Add(ht);
                    }
                }
            }
            ph.Data = listht;
            return(ph);
        }
Esempio n. 4
0
        private static string GetKey <TEntity, TOutputDto>(IQueryable <TEntity> source,
                                                           Expression <Func <TEntity, bool> > predicate,
                                                           PageCondition pageCondition,
                                                           params object[] keyParams)
            where TOutputDto : IOutputDto
        {
            source = source.Where(predicate);
            SortCondition[] sortConditions = pageCondition.SortConditions;
            if (sortConditions == null || sortConditions.Length == 0)
            {
                if (typeof(TEntity).IsEntityType())
                {
                    source = source.OrderBy("Id");
                }
                else if (typeof(TEntity).IsBaseOn <ICreatedTime>())
                {
                    source = source.OrderBy("CreatedTime");
                }
                else
                {
                    throw new OsharpException($"类型“{typeof(TEntity)}”未添加默认排序方式");
                }
            }
            else
            {
                int count = 0;
                IOrderedQueryable <TEntity> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <TEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }

                source = orderSource;
            }

            int pageIndex = pageCondition.PageIndex, pageSize = pageCondition.PageSize;

            source = source != null
                ? source.Skip((pageIndex - 1) *pageSize).Take(pageSize)
                : Enumerable.Empty <TEntity>().AsQueryable();

            IQueryable <TOutputDto> query = source.ToOutput <TEntity, TOutputDto>(true);

            return(GetKey(query.Expression, keyParams));
        }
        /// <summary>
        /// 把<see cref="IOrderedQueryable{T}"/>集合继续按指定字段排序方式进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="propertyName">排序属性名</param>
        /// <param name="sortDirection">排序方向</param>
        /// <returns></returns>
        public static IOrderedQueryable <T> ThenBy <T>(this IOrderedQueryable <T> source,
                                                       string propertyName,
                                                       ListSortDirection sortDirection = ListSortDirection.Ascending)
        {
            if (!new NotNullOrEmpty().IsValid(propertyName))
            {
                throw new ArgumentNullException(nameof(propertyName));
            }

            if (!new NotNull().IsValid(source))
            {
                throw new ArgumentNullException(nameof(source));
            }

            return(CollectionPropertySorter <T> .ThenBy(source, propertyName, sortDirection));
        }
        /// <summary>
        /// 从指定<see cref="IQueryable{T}"/>集合中查询指定分页条件的子数据集
        /// </summary>
        /// <typeparam name="TEntity">动态实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="total">输出符合条件的总记录数</param>
        /// <param name="sortConditions">排序条件集合</param>
        /// <returns></returns>
        public static IQueryable <TEntity> Where <TEntity>(this IQueryable <TEntity> source,
                                                           Expression <Func <TEntity, bool> > predicate,
                                                           int pageIndex,
                                                           int pageSize,
                                                           out int total,
                                                           SortCondition[] sortConditions = null)
        {
            source.CheckNotNull("source");
            predicate.CheckNotNull("predicate");
            pageIndex.CheckGreaterThan("pageIndex", 0);
            pageSize.CheckGreaterThan("pageSize", 0);

            total  = source.Count(predicate);
            source = source.Where(predicate);
            if (sortConditions == null || sortConditions.Length == 0)
            {
                if (typeof(TEntity).IsEntityType())
                {
                    source = source.OrderBy("Id");
                }
                else if (typeof(TEntity).IsBaseOn <ICreatedTime>())
                {
                    source = source.OrderBy("CreatedTime");
                }
                else
                {
                    throw new OsharpException($"类型“{typeof(TEntity)}”未添加默认排序方式");
                }
            }
            else
            {
                int count = 0;
                IOrderedQueryable <TEntity> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <TEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }
                source = orderSource;
            }
            return(source != null
                ? source.Skip((pageIndex - 1) *pageSize).Take(pageSize)
                : Enumerable.Empty <TEntity>().AsQueryable());
        }
Esempio n. 7
0
        private static string GetKey <TEntity, TResult>(IQueryable <TEntity> source,
                                                        Expression <Func <TEntity, bool> > predicate,
                                                        PageCondition pageCondition,
                                                        Expression <Func <TEntity, TResult> > selector)
        {
            if ((pageCondition.SortConditions == null || pageCondition.SortConditions.Length == 0) && string.IsNullOrWhiteSpace(pageCondition.PrimaryKeyField))
            {
                throw new ArgumentException("排序条件集合为空的话,请设置主键字段数值!");
            }

            source = source.Where(predicate);
            SortCondition[] _sortConditions = pageCondition.SortConditions;

            if (_sortConditions == null || _sortConditions.Length == 0)
            {
                source = source.OrderBy(pageCondition.PrimaryKeyField);
            }

            else
            {
                int _count = 0;
                IOrderedQueryable <TEntity> orderSource = null;

                foreach (SortCondition sortCondition in _sortConditions)
                {
                    orderSource = _count == 0
                                  ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                                  : CollectionPropertySorter <TEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    _count++;
                }

                source = orderSource;
            }

            int _pageIndex = pageCondition.PageIndex, pageSize = pageCondition.PageSize;

            source = source != null
                     ? source.Skip((_pageIndex - 1) *pageSize).Take(pageSize)
                     : Enumerable.Empty <TEntity>().AsQueryable();

            IQueryable <TResult> _query = source.Select(selector);

            return(GetKey(_query.Expression));
        }
Esempio n. 8
0
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <param name="sortConditions"></param>
        /// <returns></returns>
        public virtual List <T> GetList <T>(Expression <Func <TEntity, bool> > predicate,
                                            int pageIndex,
                                            int pageSize,
                                            ref int total,
                                            List <SortCondition> sortConditions = null) where T : IViewModel, new()
        {
            //source.CheckNotNull("source");
            //predicate.CheckNotNull("predicate");
            //pageIndex.CheckGreaterThan("pageIndex", 0);
            //pageSize.CheckGreaterThan("pageSize", 0);

            if (total <= 0)
            {
                total = Repository.Entities.Count(predicate);
            }
            var source = Repository.Entities.Where(predicate);

            if (sortConditions == null || sortConditions.Count == 0)
            {
                source = source.OrderByDescending(m => m.Id);
            }
            else
            {
                int count = 0;
                IOrderedQueryable <TEntity> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <TEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }
                source = orderSource;
            }
            var lst = source != null
                ? source.Skip((pageIndex - 1) *pageSize).Take(pageSize)
                : Enumerable.Empty <TEntity>();

            return(lst.ToList().Select(n => (T)(new T().ConvertAPIModel(n))).ToList());

            //  var lst = this.Entities.Where(predicate).Take(iTop).ToList().Select(n => (T)(new T().ConvertAPIModel(n))).ToList();

            // return lst;
        }
Esempio n. 9
0
        /// <summary>
        /// 从指定<see cref="IQueryable{T}"/>集合 中查询指定分页条件的子数据集
        /// </summary>
        /// <typeparam name="TEntity">动态实体类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="total">输出符合条件的总记录数</param>
        /// <param name="sortConditions">排序条件集合</param>
        /// <returns></returns>
        public static IQueryable <TEntity> Where <TEntity, TKey>(this IQueryable <TEntity> source,
                                                                 Expression <Func <TEntity, bool> > predicate,
                                                                 int pageIndex,
                                                                 int pageSize,
                                                                 out int total,
                                                                 SortCondition[] sortConditions = null) where TEntity : EntityBase <TKey>
        {
            source.CheckNotNull("source");
            predicate.CheckNotNull("predicate");
            pageIndex.CheckGreaterThan("pageIndex", 0);
            pageSize.CheckGreaterThan("pageSize", 0);

            if ((predicate.Type.FullName.Contains("Mes.Demo.Models.TestLog.Cpk")) && (predicate.Body.ToString() == "True"))
            {
                total = 10000000;
            }
            else
            {
                total = source.Count(predicate);
            }
            source = source.Where(predicate);
            if (sortConditions == null || sortConditions.Length == 0)
            {
                source = source.OrderBy(m => m.Id);
            }
            else
            {
                int count = 0;
                IOrderedQueryable <TEntity> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <TEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }
                source = orderSource;
            }
            return(source != null
                ? source.Skip((pageIndex - 1) *pageSize).Take(pageSize)
                : Enumerable.Empty <TEntity>().AsQueryable());
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="pageCon"></param>
        /// <returns></returns>
        public List <T> GetListWithContent <T>(Expression <Func <ArticleInfo, bool> > predicate, PageCondition pageCon)
        {
            var pageIndex      = pageCon.PageIndex;
            var pageSize       = pageCon.PageSize;
            var total          = pageCon.RowCount;
            var sortConditions = pageCon.SortConditions;

            if (total <= 0)
            {
                total            = Repository.Entities.Count(predicate);
                pageCon.RowCount = total;
            }
            var source = Repository.Entities.Where(predicate);

            if (sortConditions == null || sortConditions.Count == 0)
            {
                source = source.OrderByDescending(m => m.Id);
            }
            else
            {
                int count = 0;
                IOrderedQueryable <ArticleInfo> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <ArticleInfo> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <ArticleInfo> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }
                source = orderSource;
            }
            var lst = source != null
                ? source.Skip((pageIndex - 1) *pageSize).Take(pageSize)
                : Enumerable.Empty <ArticleInfo>();

            return(lst.ToList().Select(n => (T)(new ArticleInfoView().ConvertAPIModelListWithContent(n))).ToList());

            //  var lst = this.Entities.Where(predicate).Take(iTop).ToList().Select(n => (T)(new T().ConvertAPIModel(n))).ToList();

            // return lst;
        }
Esempio n. 11
0
        private static string GetKey <TEntity, TOutputDto>(IQueryable <TEntity> source,
                                                           Expression <Func <TEntity, bool> > predicate,
                                                           PageCondition pageCondition,
                                                           params object[] keyParams)
            where TOutputDto : IOutputDto
        {
            if (!typeof(TEntity).IsEntityType())
            {
                throw new InvalidOperationException(Resources.QueryCacheExtensions_TypeNotEntityType.FormatWith(typeof(TEntity).FullName));
            }

            source = source.Where(predicate);
            SortCondition[] sortConditions = pageCondition.SortConditions;
            if (sortConditions == null || sortConditions.Length == 0)
            {
                source = source.OrderBy("Id");
            }
            else
            {
                int count = 0;
                IOrderedQueryable <TEntity> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <TEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }
                source = orderSource;
            }
            int pageIndex = pageCondition.PageIndex, pageSize = pageCondition.PageSize;

            source = source != null
                ? source.Skip((pageIndex - 1) *pageSize).Take(pageSize)
                : Enumerable.Empty <TEntity>().AsQueryable();

            IQueryable <TOutputDto> query = source.ToOutput <TOutputDto>();

            return(GetKey(query.Expression, keyParams));
        }
Esempio n. 12
0
        /// <summary>
        /// 从指定<see cref="IQueryable{T}"/>集合中查询指定分页条件的子数据集
        /// </summary>
        /// <typeparam name="TEntity">动态实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="total">输出符合条件的总记录数</param>
        /// <param name="sortConditions">排序条件集合</param>
        /// <returns></returns>
        public static IQueryable <TEntity> Where <TEntity>(this IQueryable <TEntity> source,
                                                           Expression <Func <TEntity, bool> > predicate,
                                                           int pageIndex,
                                                           int pageSize,
                                                           out int total,
                                                           SortCondition[] sortConditions = null)
        {
            source.CheckNotNull("source");
            predicate.CheckNotNull("predicate");
            pageIndex.CheckGreaterThan("pageIndex", 0);
            pageSize.CheckGreaterThan("pageSize", 0);

            if (!typeof(TEntity).IsEntityType())
            {
                throw new InvalidOperationException(Resources.QueryCacheExtensions_TypeNotEntityType.FormatWith(typeof(TEntity).FullName));
            }

            total  = source.Count(predicate);
            source = source.Where(predicate);
            if (sortConditions == null || sortConditions.Length == 0)
            {
                source = source.OrderBy("Id");
            }
            else
            {
                int count = 0;
                IOrderedQueryable <TEntity> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <TEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }
                source = orderSource;
            }
            return(source != null
                ? source.Skip((pageIndex - 1) *pageSize).Take(pageSize)
                : Enumerable.Empty <TEntity>().AsQueryable());
        }
        /// <summary>
        /// 从指定<see cref="IQueryable{T}"/>集合中查询指定分页条件的子数据集
        /// </summary>
        /// <typeparam name="TEntity">动态实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="primaryKeyField">主键字段</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="total">输出符合条件的总记录数</param>
        /// <param name="sortConditions">排序条件集合</param>
        /// <returns></returns>
        public static IQueryable <TEntity> Where <TEntity>(this IQueryable <TEntity> source,
                                                           Expression <Func <TEntity, bool> > predicate,
                                                           string primaryKeyField,
                                                           int pageIndex,
                                                           int pageSize,
                                                           out int total,
                                                           SortCondition[] sortConditions = null)
        {
            if ((sortConditions == null || sortConditions.Length == 0) && string.IsNullOrWhiteSpace(primaryKeyField))
            {
                throw new ArgumentException("排序条件集合为空的话,请设置主键字段数值!");
            }

            total  = source.Count(predicate);
            source = source.Where(predicate);

            if (sortConditions == null || sortConditions.Length == 0)
            {
                source = source.OrderBy(primaryKeyField);
            }
            else
            {
                int _count = 0;
                IOrderedQueryable <TEntity> _orderSource = null;

                foreach (SortCondition sortCondition in sortConditions)
                {
                    _orderSource = _count == 0
                                   ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                                   : CollectionPropertySorter <TEntity> .ThenBy(_orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    _count++;
                }

                source = _orderSource;
            }

            return(source != null
                   ? source.Skip((pageIndex - 1) *pageSize).Take(pageSize)
                   : Enumerable.Empty <TEntity>().AsQueryable());
        }
Esempio n. 14
0
        public override List <T> GetList <T>(Expression <Func <ArticleInfo, bool> > predicate,
                                             int pageIndex,
                                             int pageSize,
                                             ref int total,
                                             List <SortCondition> sortConditions = null)
        {
            if (total <= 0)
            {
                total = Entities.Count(predicate);
            }
            var source = Entities.Where(predicate);

            if (sortConditions == null || sortConditions.Count == 0)
            {
                source = source.OrderByDescending(m => m.Id);
            }
            else
            {
                int count = 0;
                IOrderedQueryable <ArticleInfo> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <ArticleInfo> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <ArticleInfo> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }
                source = orderSource;
            }
            var lst = source != null
                ? source.Skip((pageIndex - 1) *pageSize).Take(pageSize)
                : Enumerable.Empty <ArticleInfo>();

            return(lst.ToList().Select(n => (T)(new T().ConvertAPIModel(n))).ToList());

            //  var lst = this.Entities.Where(predicate).Take(iTop).ToList().Select(n => (T)(new T().ConvertAPIModel(n))).ToList();

            // return lst;
        }
Esempio n. 15
0
        private static string GetKey <TEntity, TResult>(IQueryable <TEntity> source,
                                                        Expression <Func <TEntity, bool> > predicate,
                                                        PageCondition pageCondition,
                                                        Expression <Func <TEntity, TResult> > selector)
        {
            if (!typeof(TEntity).IsEntityType())
            {
                throw new InvalidOperationException("类型“{0}”不是实体类型".FormatWith(typeof(TEntity).FullName));
            }

            source = source.Where(predicate);
            SortCondition[] sortConditions = pageCondition.SortConditions;
            if (sortConditions == null || sortConditions.Length == 0)
            {
                source = source.OrderBy("Id");
            }
            else
            {
                int count = 0;
                IOrderedQueryable <TEntity> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <TEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }
                source = orderSource;
            }
            int pageIndex = pageCondition.PageIndex, pageSize = pageCondition.PageSize;

            source = source != null
                ? source.Skip((pageIndex - 1) *pageSize).Take(pageSize)
                : Enumerable.Empty <TEntity>().AsQueryable();

            IQueryable <TResult> query = source.Select(selector);

            return(GetKey(query.Expression));
        }
        /// <summary>
        /// 把排序条件集合转成排序查询
        /// </summary>
        /// <typeparam name="TEntity">要排序实体</typeparam>
        /// <param name="source">源</param>
        /// <param name="orderConditions">排序条件集合</param>
        /// <returns></returns>
        public static IOrderedQueryable <TEntity> OrderBy <TEntity>(this IQueryable <TEntity> source, OrderCondition[] orderConditions)
        {
            IOrderedQueryable <TEntity> orderSource = null;

            if (orderConditions == null || orderConditions.Length == 0)
            {
                orderSource = CollectionPropertySorter <TEntity> .OrderBy(source, "Id", SortDirection.Ascending);
            }

            int count = 0;

            foreach (OrderCondition orderCondition in orderConditions)
            {
                orderSource = count == 0
                    ? CollectionPropertySorter <TEntity> .OrderBy(source, orderCondition.SortField, orderCondition.SortDirection)
                    : CollectionPropertySorter <TEntity> .ThenBy(orderSource, orderCondition.SortField, orderCondition.SortDirection);

                count++;
            }

            return(orderSource);
        }
        public void OrderByTest()
        {
            IEnumerable <TestEntity> list1 = Entities.ToList();
            IEnumerable <TestEntity> list2 = CollectionPropertySorter <TestEntity> .OrderBy(list1, "AddDate", ListSortDirection.Ascending);

            Assert.False(list2.SequenceEqual(list1));
            IOrderedEnumerable <TestEntity> temp1 = CollectionPropertySorter <TestEntity> .OrderBy(list1, "AddDate", ListSortDirection.Ascending);

            IEnumerable <TestEntity> list3 = CollectionPropertySorter <TestEntity> .ThenBy(temp1, "IsDeleted", ListSortDirection.Descending);

            Assert.False(list1.SequenceEqual(list3));

            IQueryable <TestEntity> query1 = Entities.AsQueryable();
            IQueryable <TestEntity> query2 = CollectionPropertySorter <TestEntity> .OrderBy(query1, "AddDate", ListSortDirection.Ascending);

            Assert.False(query2.SequenceEqual(query1));
            IOrderedQueryable <TestEntity> temp2 = CollectionPropertySorter <TestEntity> .OrderBy(query1, "AddDate", ListSortDirection.Ascending);

            IEnumerable <TestEntity> query3 = CollectionPropertySorter <TestEntity> .ThenBy(temp2, "IsDeleted", ListSortDirection.Descending);

            Assert.False(query1.SequenceEqual(query3));
        }
        /// <summary>
        /// 从指定<see cref="IQueryable{T}"/>集合 中查询指定分页条件的子数据集
        /// </summary>
        /// <typeparam name="TEntity">动态实体类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="total">输出符合条件的总记录数</param>
        /// <param name="sortConditions">排序条件集合</param>
        /// <returns></returns>
        public static IQueryable <TEntity> Where <TEntity, TKey>(this IQueryable <TEntity> source,
                                                                 Expression <Func <TEntity, bool> > predicate,
                                                                 int pageIndex,
                                                                 int pageSize,
                                                                 out int total,
                                                                 List <SortCondition> sortConditions = null) where TEntity : EntityBase <TKey>
        {
            source.CheckNotNull("source");
            predicate.CheckNotNull("predicate");
            pageIndex.CheckGreaterThan("pageIndex", 0);
            pageSize.CheckGreaterThan("pageSize", 0);

            total  = source.Count(predicate);
            source = source.Where(predicate);
            if (sortConditions == null || sortConditions.Count == 0)
            {
                source = source.OrderBy(m => m.Id);
            }
            else
            {
                int count = 0;
                IOrderedQueryable <TEntity> orderSource = null;
                foreach (SortCondition sortCondition in sortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <TEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }
                source = orderSource;
            }
            return(source != null
                ? source.Skip((pageIndex - 1) *pageSize).Take(pageSize)
                : Enumerable.Empty <TEntity>().AsQueryable());
        }
Esempio n. 19
0
        public IList <HseMessageView> GetHseStatus(RequestMessage requestMessage, PageCondition pageCondition, bool dataExport = false)
        {
            var before30Days = DateTime.Now.AddDays(-MonthRange);
            var startDate    = new DateTime(before30Days.Year, before30Days.Month, before30Days.Day);

            var expression = GetExpression(requestMessage, startDate);

            //expression = expression.And(x => x.CreatedDate >= startDate);

            var allQuery = GetDistinctQueryable(expression);

            var totalFutureQuery = allQuery.FutureCount();
            var query            = allQuery;

            if (pageCondition.SortConditions == null || !pageCondition.SortConditions.Any())
            {
                query = query.OrderByDescending(x => x.Id);
            }
            else
            {
                var count = 0;
                IOrderedQueryable <HseReceiverUserEntity> orderSource = null;
                foreach (var sortCondition in pageCondition.SortConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter <HseReceiverUserEntity> .OrderBy(query, sortCondition.SortField, sortCondition.ListSortDirection)
                        : CollectionPropertySorter <HseReceiverUserEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection);

                    count++;
                }
                query = orderSource;
            }

            FutureQuery <HseReceiverUserEntity> listFutureQuery;

            if (pageCondition.PageSize <= 0)
            {
                listFutureQuery = query.Future();
            }
            else
            {
                listFutureQuery = (query != null ? query.Skip((pageCondition.PageIndex - 1) * pageCondition.PageSize).Take(pageCondition.PageSize) : Enumerable.Empty <HseReceiverUserEntity>()).AsQueryable().Future();
            }

            List <HseReceiverUserEntity> list;

            using (var trans = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadUncommitted
            }))
            {
                list = listFutureQuery.ToList();

                pageCondition.RowCount = totalFutureQuery.Value;
                trans.Complete();
            }

            if (!list.Any())
            {
                return(new List <HseMessageView>());
            }


            var repliedMessages = Repository.Entities.Where(
                x => x.RepliedDate >= startDate).Select(x => new { x.LillyId, x.Tel, x.Content, x.RepliedDate, x.Id, x.Origin, x.Status }).OrderByDescending(x => x.Id).ToList().Select(x => new HseReplyMessage {
                LillyId = x.LillyId, Tel = x.Tel, Content = x.Content, RepliedDate = x.RepliedDate, Status = x.Status, Origin = (Origin)Enum.Parse(typeof(Origin), x.Origin, true)
            }).ToList();

            var repliedGroup = repliedMessages.GroupBy(x => x.LillyId);

            return(list.AsParallel().Select(x =>
            {
                var items = repliedGroup.Where(y => y.Key.Equals(x.LillyId, StringComparison.OrdinalIgnoreCase)).SelectMany(y => y).ToList();

                if (dataExport == false)
                {
                    items.ForEach(t =>
                                  { t.Content = ConvertEmotion(t.Content); }
                                  );
                }

                HseReplyMessage latestItem = null;
                if (items.Any())
                {
                    latestItem = items[0];
                }

                return new HseMessageView
                {
                    LillyId = x.LillyId,
                    Name = x.Name,
                    Tel = x.Tel,
                    OneDepartment = x.FristLevelDepartmentName,
                    TwoDepartment = x.SecondLevelDepartmentName,
                    ThreeDepartment = x.ThirdLevelDepartmentName,
                    Location = x.Location,
                    EmergencyContact = x.EmergencyContact,
                    EmergencyName = x.EmergencyName,
                    ReplyMessages = items,
                    LatestRepliedDate = latestItem != null ? (DateTime?)latestItem.RepliedDate : null,
                    Status = x.Status,
                    LatestRepliedContent = latestItem != null ? latestItem.Content : null,
                };
            }).ToList());
        }
 /// <summary>
 /// 把<see cref="IOrderedQueryable{T}"/>集合继续按指定字段排序方式进行排序
 /// </summary>
 /// <typeparam name="T">动态类型</typeparam>
 /// <param name="source">要排序的数据集</param>
 /// <param name="propertyName">排序属性名</param>
 /// <param name="sortDirection">排序方向</param>
 /// <returns></returns>
 public static IOrderedQueryable <T> ThenBy <T>(this IOrderedQueryable <T> source,
                                                string propertyName,
                                                ListSortDirection sortDirection = ListSortDirection.Ascending)
 {
     return(CollectionPropertySorter <T> .ThenBy(source, propertyName, sortDirection));
 }