MvcContrib网格排序 [英] MvcContrib Grid Sorting

查看:85
本文介绍了MvcContrib网格排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试MvcContrib的网格以进行排序.

Am testing out MvcContrib's grid for sorting.

我将LightSpeed用作我的ORM

Am using LightSpeed as my ORM

问题:出现以下错误:listOfRfidTags = ...

Problem: getting compile error on: listOfRfidTags = ...

无法从用法中推断方法'System.Linq.Enumerable.OrderBy(System.Collections.Generic.IEnumerable,System.Func,System.Collections.Generic.IComparer)'的类型参数.尝试显式指定类型参数.

The type arguments for method 'System.Linq.Enumerable.OrderBy(System.Collections.Generic.IEnumerable, System.Func, System.Collections.Generic.IComparer)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

public ActionResult Index(GridSortOptions sort)
        {
            IEnumerable<RfidTag> listOfRfidTags = uow.RfidTags;
            if(sort.Column != null) {
                listOfRfidTags = listOfRfidTags.OrderBy(sort.Column, sort.Direction);
            }
            ViewData["sort"] = sort;
            return View(listOfRfidTags);
        }

视图:

@Html.Grid(Model).Columns(column =>{
    column.For(a => Html.ActionLink("Edit", "Edit", new { id = a.Id })).Named("Edit");
    column.For(a => a.TagCode).Named("TagCode").Sortable(true);
    column.For(a => a.Number);
})

推荐答案

OrderBy扩展方法采用委托来获取排序键,而不是列和方向.所以这行:

The OrderBy extension method takes a delegate for getting the sort key, not a column and direction. So this line:

listOfRfidTags = listOfRfidTags.OrderBy(sort.Column, sort.Direction);

需要看起来像这样:

listOfRfidTags = listOfRfidTags.OrderBy(r => r.SomeProperty);

(或OrderByDescending,取决于sort.Direction).问题在于,无法在编译时确定SomeProperty,因为您希望它来自sort.Column.这意味着,如果要使用LINQ,则可能需要使用

(or OrderByDescending depending on the sort.Direction). The trouble is that SomeProperty can't be determined at compile time because you want it to come from sort.Column. This means that if you want to use LINQ then you'll probably need to use Dynamic LINQ or Reflection to extract the property you want to sort on e.g.

PropertyInfo property = typeof(RfidTag).GetProperty(sort.Column);
listOfRfidTags = listOfRfidTags.OrderBy(r => property.GetValue(r));

但是,由于您将LightSpeed用作ORM,因此您可以绕过LINQ并使用核心API,该API 允许使用动态列名:

However, since you are using LightSpeed as your ORM, you can bypass LINQ and use the core API, which does allow dynamic column names:

Order order = Order.By(sort.Column);
if (sort.Direction == SortDirection.Descending))
  order = order.Descending();
IList<RfidTag> listOfRfidTags = uow.Find<RfidTag>(new Query { Order = order });

这有一个好处,即排序将在数据库而不是Web应用程序中进行.

This has the side benefit that the sorting will happen on the database instead of in the Web application.

这篇关于MvcContrib网格排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆