用jqGrid难于排序Linq表达式 [英] Difficulty sorting Linq expression with jqGrid

查看:105
本文介绍了用jqGrid难于排序Linq表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这段代码

I have seen this code Sortable JqGrid using LINQ to MySQL (DbLinq) and Dynamic LINQ - Orderby doesn't work and trying to make it work. But it's giving error:

无法按"System.Object"类型订购

Cannot order by type 'System.Object'

在代码中看到这里有错误"的行.

See the line in the code with Error Here.

[HttpPost]
public ActionResult MyGridData(string sidx, string sord, int page, int rows)
{
    IQueryable<Company> repository = companyRepository.GetGridCompanies();

    int pageIndex = Convert.ToInt32(page) - 1;
    int pageSize = rows;
    int totalRecords = repository.Count();
    int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

    // first sorting the data as IQueryable<Ticket> without converting ToList()
    IQueryable<Company> orderdData = repository;

    PropertyInfo propertyInfo = typeof(Company).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);
    }

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

    var jsonData = new
    {
        total = totalPages,
        page,
        records = totalRecords,
        rows = (
            from o in pagedData  //ERROR HERE : Cannot order by type 'System.Object'
            select new
            {
                i = o.companyID,
                cell = new string[] { o.companyID.ToString(), o.companyName, o.companyCity, o.companyState }
            }).ToArray()
    };
    return Json(jsonData);
}

public class CompanyRepository
{
    SandGridDataContext db = new SandGridDataContext();

    // Send Total Number of Companies
    public int CompanyCount()
    {
        return db.Companies.Count();
    }

    public IQueryable<Company> GetGridCompanies()
    {
        return db.Companies;
    }

    public Company GetCompany(int id)
    {
        return db.Companies.SingleOrDefault(d => d.companyID == id);
    }

}

//JS代码

jQuery().ready(function () {
    var lastSel;
    jQuery("#sandgrid").jqGrid({
        url: '/JQSandbox/MyGridData/',
        datatype: 'json',
        mtype: 'POST',
        height: 255,
        width: 640,
        colNames: ['Index', 'Name', 'City', 'State'],
        colModel: [
                { name: 'companyID', index: 'companyID', width: 5 },
                { name: 'companyName', index: 'companyName', width: 30 },
                { name: 'companyCity', index: 'companyCity', width: 30 },
                { name: 'companyState', index: 'companyState', width: 4, sortable: false}],

        pager: jQuery('#sandgridp'),
        rowNum: 5,
        rowList: [5, 10, 20, 50],
        sortname: 'companyID',
        sortorder: "desc",
        viewrecords: true,
        altRows: true,
        caption: 'Sandbox Grid',
        ondblClickRow: function (id) {
            if (id && id !== lastSel) {
                jQuery('#sandgrid').restoreRow(lastSel);
                lastSel = id;
                alert("You've seleted " + id);
            }
        },
        subGrid: true,
        subGridUrl: '/JQSandbox/MySubGridData/',
        subGridModel: [
        {
            name: ['Name', 'Department', 'Hire Date', 'Supervisor'],
            width: [80, 20, 80, 10],
            align: ['left', 'left', 'left', 'center'],
            params: ['companyID']
        }]
    }).navGrid('#sandgridp', { edit: false, add: false, del: false });

///Company类是Linq to Sql实体,其字段如下.

companyID,
companyName, 
companyCity,
companyState

推荐答案

我花了很多时间来解决这个问题. 请检查通过"string"订购LINQ名称.

I spent too much time to resolve this. Please check Order LINQ by "string" name.

这篇关于用jqGrid难于排序Linq表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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