加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – MVCGrid.Net排序问题 – sortdirection

发布时间:2020-12-16 09:55:38 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试使用MVCGrid.Net设置网格,但我使用排序的代码给出了options.sortdirection的错误. public static void RegisterGrids() { MVCGridDefinitionTable.Add("CustomerGrid",new MVCGridBuilderCustomer() .WithAuthorizationType(AuthorizationType.All
我正在尝试使用MVCGrid.Net设置网格,但我使用排序的代码给出了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;
            })
        );
    }

错误发生在case语句中的查询中.请尽可能协助.
视觉工作室给出的错误是:

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
TestsMVCGridaMVCGridaApp_StartMVCGridConfig.cs 39 46 MVCGrida

解决方法

我也有同样的问题.我不确定教程中的Entity Framework示例是否返回与标准IEnumerable不同的东西,或者它是否只是教程中的错误.无论哪种方式,我必须在我的情况下手动解决排序.这不是世界上最漂亮的,但它确实有效.

例如:

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;                                 
 }

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读