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

c# – 使用LINQ to MySQL(DbLinq)和动态LINQ的可排序JqGrid – O

发布时间:2020-12-16 01:21:34 所属栏目:百科 来源:网络整理
导读:我在JqGrid中排序条目时遇到问题. Orderby似乎不起作用.我在代码中设置了断点,我注意到,orderby不会改变元素的顺序.知道什么可能是错的吗? 我正在使用LINQ to SQL with MySQL(DbLinq项目). 我的行动代码: public ActionResult All(string sidx,string sord
我在JqGrid中排序条目时遇到问题. Orderby似乎不起作用.我在代码中设置了断点,我注意到,orderby不会改变元素的顺序.知道什么可能是错的吗?

我正在使用LINQ to SQL with MySQL(DbLinq项目).

我的行动代码:

public ActionResult All(string sidx,string sord,int page,int rows)
        {
            var tickets = ZTRepository.GetAllTickets().OrderBy(sidx + " " + sord).ToList();
            var rowdata = (
                from ticket in tickets
                select new {
                    i = ticket.ID,cell = new String[] {
                        ticket.ID.ToString(),ticket.Hardware,ticket.Issue,ticket.IssueDetails,ticket.RequestedBy,ticket.AssignedTo,ticket.Priority.ToString(),ticket.State
                    }
                }).ToArray();

            var jsonData = new
            {
                total = 1,// we'll implement later 
                page = page,records = tickets.Count(),rows = rowdata
            };

            return Json(jsonData,JsonRequestBehavior.AllowGet);
        }

解决方法

尝试以下方法

public ActionResult All(string sidx,int rows)
{
    IQueryable<Ticket> repository = ZTRepository.GetAllTickets();
    int totalRecords = repository.Count();

    // first sorting the data as IQueryable<Ticket> without converting ToList()
    IQueryable<Ticket> orderdData = repository;
    System.Reflection.PropertyInfo propertyInfo =
        typeof(Ticket).GetProperty (sidx);
    if (propertyInfo != null) {
        orderdData = String.Compare(sord,"desc",StringComparison.Ordinal) == 0 ?
            (from x in repository
             orderby propertyInfo.GetValue (x,null) descending
             select x) :
            (from x in repository
             orderby propertyInfo.GetValue (x,null)
             select x);
    }
    // if you use fields instead of properties,then one can modify the code above
    // to the following
    // System.Reflection.FieldInfo fieldInfo =
    //         typeof(Ticket).GetField (sidx);
    // if (fieldInfo != null) {
    //  orderdData = String.Compare(sord,StringComparison.Ordinal) == 0 ?
    //      (from x in repository
    //       orderby fieldInfo.GetValue (x,null) descending
    //       select x) :
    //      (from x in repository
    //       orderby fieldInfo.GetValue (x,null)
    //       select x);
    //}

    // paging of the results
    IQueryable<Ticket> pagedData = orderdData
        .Skip ((page > 0? page - 1: 0) * rows)
        .Take (rows);

    // now the select statement with both sorting and paging is prepared
    // and we can get the data
    var rowdata = ( from ticket in tickets
                    select new {
                        id = ticket.ID,cell = new String[] {
                            ticket.ID.ToString(),ticket.State
                        }
                    }).ToList();                

    var jsonData = new {
        total = page,records = totalRecords,total = (totalRecords + rows - 1) / rows,rows = pagedData
    };

    return Json(jsonData,JsonRequestBehavior.AllowGet);
}

在这里,我假设您的票证对象的类型是Ticket.

(编辑:李大同)

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

    推荐文章
      热点阅读