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); }
/// <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); }