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

            if (sortDescEnumerator.MoveNext())
            {
                try
                {
                    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);
        }
Ejemplo n.º 2
0
 public IEnumerable <SortDesc> GetSorts()
 {
     if (string.IsNullOrWhiteSpace(Sort))
     {
         yield break;
     }
     foreach (string str in Sort.Split(new[] { ',', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries))
     {
         var sd = SortDesc.Parse(str);
         yield return(sd);
     }
 }
Ejemplo n.º 3
0
        public static SortDesc Parse(string str)
        {
            var sd = new SortDesc {
                PropertyName = str
            };

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

            return(sd);
        }