Note that there are some explanatory texts on larger screens.

plurals
  1. PODifficulty sorting Linq expression with jqGrid
    primarykey
    data
    text
    <p>I have seen this code <a href="https://stackoverflow.com/questions/4078592/sortable-jqgrid-using-linq-to-mysql-dblinq-and-dynamic-linq-orderby-doesnt-w">Sortable JqGrid using LINQ to MySQL (DbLinq) and Dynamic LINQ - Orderby doesn&#39;t work</a> and trying to make it work. But it's giving error:</p> <blockquote> <p>Cannot order by type 'System.Object'</p> </blockquote> <p>See the line in the code with Error Here.</p> <pre><code>[HttpPost] public ActionResult MyGridData(string sidx, string sord, int page, int rows) { IQueryable&lt;Company&gt; 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&lt;Ticket&gt; without converting ToList() IQueryable&lt;Company&gt; 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&lt;Company&gt; pagedData = orderdData .Skip((page &gt; 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&lt;Company&gt; GetGridCompanies() { return db.Companies; } public Company GetCompany(int id) { return db.Companies.SingleOrDefault(d =&gt; d.companyID == id); } } </code></pre> <p><strong>//JS code</strong></p> <pre><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 &amp;&amp; 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 }); </code></pre> <p><strong>//Company class is Linq to Sql entity with fields below.</strong></p> <pre><code>companyID, companyName, companyCity, companyState </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload