Exemplo n.º 1
0
        public IQueryable <T> ApplySort <T>(IQueryable <T> query, string defaultSort = null)
        {
            var sortDescEnumerator = GetSorts().GetEnumerator();

            if (sortDescEnumerator.MoveNext())
            {
                try
                {
                    // extensions from https://gist.github.com/zaverden/c2d611f33f60cbe8234b420d87671c10
                    IOrderedQueryable <T> orderedQuery = query.OrderBy(sortDescEnumerator.Current.PropertyName, sortDescEnumerator.Current.Desc);

                    while (sortDescEnumerator.MoveNext())
                    {
                        orderedQuery = orderedQuery.ThenBy(sortDescEnumerator.Current.PropertyName, sortDescEnumerator.Current.Desc);
                    }

                    return(orderedQuery);
                }
                catch (MissingMemberException exc)
                {
                    throw new WrongSortPropertyException(sortDescEnumerator.Current.PropertyName, exc);
                }
            }

            if (defaultSort != null)
            {
                var ds = SortDesc.Parse(defaultSort);
                IOrderedQueryable <T> orderedQuery = query.OrderBy(ds.PropertyName, ds.Desc);
                return(orderedQuery);
            }

            return(query);
        }
Exemplo n.º 2
0
 public IEnumerable <SortDesc> GetSorts()
 {
     if (!string.IsNullOrWhiteSpace(Sort))
     {
         foreach (string str in Sort.Split(new[] { ',', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries))
         {
             var sd = SortDesc.Parse(str);
             yield return(sd);
         }
     }
 }