/// <summary> /// Validates orderby string and constructs SortInformation object. If not valid returns null. /// </summary> /// <param name="orderBy"></param> /// <param name="validOrderByOptions"></param> /// <returns>If order is not valid null, otherwise insatnce of <see cref="SortInformation"/></returns> protected SortInformation ParseOrderBy(string orderBy, string validOrderByOptions) { if (string.IsNullOrWhiteSpace(orderBy)) { return(null); } SortInformation sortInformation = GetSortInformation(orderBy); if (validOrderByOptions.Contains(sortInformation.PropertyName)) { return(sortInformation); } return(null); }
public override IQueryable <Contact> SetUpSorting(IQueryable <Contact> query) { SortInformation sortInformation = ParseOrderBy(OrderBy, ValidOrderByValues); if (sortInformation == null) { return(query); } switch (sortInformation.PropertyName) { case "name": query = ApplyOrdering(query, s => s.Name, sortInformation.SortDirection); break; case "address": query = ApplyOrdering(query, s => s.Address, sortInformation.SortDirection); break; } return(query); }
private SortInformation GetSortInformation(string orderby) { SortInformation sortInformation; if (orderby.StartsWith("-")) { sortInformation = new SortInformation { PropertyName = orderby.Substring(1, orderby.Length - 1), SortDirection = ListSortDirection.Descending }; } else { sortInformation = new SortInformation { PropertyName = orderby, SortDirection = ListSortDirection.Ascending }; } return(sortInformation); }