Esempio n. 1
0
        public static IQueryable <TAny> Sort <TAny>(this IQueryable <TAny> list, string sortstring)
        {
            if (string.IsNullOrEmpty(sortstring))
            {
                return(list);
            }

            var sort = SortStruc <TAny> .CreateFrom(sortstring);

            //see also BaseNHibernateLinqDao.Sort,here use ThenBy while there only use OrderBy,I think they are the same.
            if (sort.Length > 0)
            {
                var orderedlist = sort[0].OrderByDirection == OrderByDirectionEnum.Asc
                        ? list.OrderBy(sort[0].OrderByExpression)
                        : list.OrderByDescending(sort[0].OrderByExpression);
                for (var i = 1; i < sort.Length; i++)
                {
                    orderedlist = sort[i].OrderByDirection == OrderByDirectionEnum.Asc
                            ? orderedlist.ThenBy(sort[i].OrderByExpression)
                            : orderedlist.ThenByDescending(sort[i].OrderByExpression);
                }
                return(orderedlist);
            }
            return(list);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sort">should be a comma delimetered string</param>
        /// <returns></returns>
        public static SortStruc <T>[] CreateFrom(string sort)
        {
            if (string.IsNullOrEmpty(sort))
            {
                return(null);
            }

            var sortStrucList = sort.Split(',');
            var a             = new SortStruc <T> [sortStrucList.Length];
            var i             = 0;

            foreach (var sortStrucString in sortStrucList)
            {
                var sortStrucPair = sortStrucString.Split(' ');
                var direction     = sortStrucPair.Length == 1 ? "asc" : sortStrucPair[1].ToLower();
                a[i++] = new SortStruc <T>
                {
                    OrderByExpression =
                        System.Linq.Dynamic.DynamicExpression.ParseLambda <T, Object>(sortStrucPair[0]),
                    OrderByDirection =
                        direction == "asc"
                                                ? OrderByDirectionEnum.Asc
                                                : OrderByDirectionEnum.Desc
                };
            }
            return(a);
        }
Esempio n. 3
0
        /// <summary>
        /// currently use linqtoobject to do sorting,so all records will be loaded.
        /// this is only for use when subquery-ordering is involved,which nhibernate dosen't surport .
        /// </summary>
        /// <typeparam name="TAny"></typeparam>
        /// <param name="list"></param>
        /// <param name="sortstring"></param>
        /// <returns></returns>
        public static IList <TAny> Sort <TAny>(this IList <TAny> list, string sortstring)
        {
            if (string.IsNullOrEmpty(sortstring))
            {
                return(list);
            }

            var sort = SortStruc <TAny> .CreateFrom(sortstring);

            if (sort.Length > 0)
            {
                var orderedlist = sort[0].OrderByDirection == OrderByDirectionEnum.Asc
                        ? list.OrderBy(sort[0].OrderByExpression.Compile())
                        : list.OrderByDescending(sort[0].OrderByExpression.Compile());
                for (var i = 1; i < sort.Length; i++)
                {
                    orderedlist = sort[i].OrderByDirection == OrderByDirectionEnum.Asc
                            ? orderedlist.ThenBy(sort[i].OrderByExpression.Compile())
                            : orderedlist.ThenByDescending(sort[i].OrderByExpression.Compile());
                }
                return(orderedlist.ToList());
            }
            return(list);
        }