0

I've written a grid component and I've decided to make having no sortable columns (on the grid) an option.

Serverside, my query does pagination using .Skip and looks similar to this

public object PaginateQueryAsJson<TQuery>(TQuery query)
        where TQuery : IQueryable<object>, IEnumerable<object>
    {
      if (page < 1)
        page = 1;
      if (pageSize < 1)
        pageSize = 1;

      int recordCount = needsCount ? query.Count() : -1;

      if (!String.IsNullOrEmpty(sortColumnName))
        query = (TQuery)query.OrderBy(sortColumnName + " " + (isAscending ? "ASC" : "DESC"));

      return new
      {
        results = query.Skip((page - 1) * pageSize)
                       .Take(pageSize).ToList(),
        recordCount = recordCount
      };
    }

My issue is that when sortColumnName is null or empty, .Skip complains with The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.

I'd like to have my helper function automatically apply a default orderby either via the first column in the table or (ideally) the primary key. I realize a solution is to explicitly specify a fallback column as an argument, but I'd like to avoid that if possible.

ekad
  • 14,436
  • 26
  • 44
  • 46
Slight
  • 1,541
  • 3
  • 19
  • 38

0 Answers0