Beispiel #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);
        }
Beispiel #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);
         }
     }
 }
Beispiel #3
0
        public static SortDesc Parse(string str)
        {
            var sd = new SortDesc {
                PropertyName = str
            };

            if (str[0] == '+' || str[0] == '-')
            {
                sd.Desc         = str[0] == '-';
                sd.PropertyName = str.Substring(1);
            }

            return(sd);
        }