0

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:

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 code

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 class is Linq to Sql entity with fields below.

companyID,
companyName, 
companyCity,
companyState
halfer
  • 19,824
  • 17
  • 99
  • 186
Pirzada
  • 4,685
  • 18
  • 60
  • 113
  • Could you include the definition of the class `Company` whith which you have problem. – Oleg Dec 19 '10 at 07:48
  • added more information about js and Company class. – Pirzada Dec 19 '10 at 10:01
  • What you added is **NOT** a class definition. Important is whether you use **properties** or **fields** for `companyID`, `companyName` and so on. If you use properties `public class Company { public int companyID { get; set; } public string companyName { get; set; } ...}` then you should use `typeof(Company).GetProperty(sidx)` in your code. If you use fields instead like `public class Company { public int companyID; public string companyName; ...}` then you should use `typeof(Company).GetField(sidx)`. My question was **how** you defined the `Company` class. – Oleg Dec 19 '10 at 19:03

1 Answers1

0

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

Community
  • 1
  • 1
Andrei Andrushkevich
  • 9,905
  • 4
  • 31
  • 42
  • 1
    answer is http://weblogs.asp.net/ricardoperes/archive/2009/12/03/dynamic-linq.aspx with System.Linq.Dynamic.dll – Pirzada Dec 19 '10 at 12:25
  • thanks, this idea with the dynamic LINQ to SQL worked for me. Just a clarification for the link posted link in the comment, that you can add "using System.Linq.Dynamic;" and all the magic works ;) – Vasil Popov Dec 18 '12 at 12:08