4

I am trying to setup a grid using MVCGrid.Net but my code using sorting is giving me an error with the options.sortdirection.

 public static void RegisterGrids()
    {
        MVCGridDefinitionTable.Add("CustomerGrid", new MVCGridBuilder<Customer>()
        .WithAuthorizationType(AuthorizationType.AllowAnonymous)
        .AddColumns(cols =>
        {
            cols.Add("Id").WithSorting(false).WithValueExpression(p => p.CustomersID.ToString());
            cols.Add("FirstName").WithHeaderText("First Name")
                .WithValueExpression(p => p.Name);
            cols.Add("Company").WithHeaderText("Company")
                .WithValueExpression(p => p.Company);
        })
        .WithSorting(true, "FirstName")
        .WithRetrieveDataMethod((context) =>
         {
            var options = context.QueryOptions;
            var result = new QueryResult<Customer>();
                using (var db = new Entities())
                {
                    var query = db.Customers.ToList();
                    if (!String.IsNullOrWhiteSpace(options.SortColumnName))
                    {
                        switch (options.SortColumnName.ToLower())
                        {
                            case "name":
                                 query = query.OrderBy(c=>c.Company, options.SortDirection);
                                break;

                        }
                    }
                    result.Items = query;
                }
                return result;
            })
        );
    }

The error is in the query in the case statement. Pls assist if possible. The error visual studio is giving is :

Error 1 'System.Collections.Generic.List' does not contain a definition for 'OrderBy' and the best extension method overload 'System.Linq.Enumerable.OrderBy(System.Collections.Generic.IEnumerable, System.Func, System.Collections.Generic.IComparer)' has some invalid arguments C:\MVC Tests\MVCGrida\MVCGrida\App_Start\MVCGridConfig.cs 39 46 MVCGrida

Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301
user3816366
  • 93
  • 2
  • 8

2 Answers2

3

I had the same issue too. I'm not sure if the Entity Framework example in the tutorial returns things differently than the standard IEnumerable, or if it's just a mistake in the tutorial. Either way, I had to manually workaround the sorting in my case. Not the prettiest in the world, but it works.

For example:

switch (options.SortColumnName.ToLower())
{
    case "name":
           if (options.SortDirection == SortDirection.Asc)
                 query = query.OrderBy(c=>c.Company);
           else if (options.SortDirection == SortDirection.Dsc)
                 query = query.OrderByDescending(c=>c.Company);    
            break;                                 
 }

Hope this helps.

mppowe
  • 215
  • 1
  • 11
0

I created an extension method to match what is on the documentation.

private static IEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, SortDirection sort)
    {
        switch (sort)
        {
            case SortDirection.Asc:
                return source.OrderBy(keySelector);

            case SortDirection.Dsc:
                return source.OrderByDescending(keySelector);

            case SortDirection.Unspecified:
            default:
                return source;
        }
    }

I use it that way

eList.OrderBy(x => x.Field, options.SortDirection);
Untherxadyus
  • 151
  • 2
  • 4