jqGrid的,问题的排序Linq的前pression [英] jqGrid, Problem with sorting Linq expression
问题描述
我已经看到了这code <一个href=\"http://stackoverflow.com/questions/4078592/sortable-jqgrid-using-linq-to-mysql-dblinq-and-dynamic-linq-orderby-doesnt-w\">Sortable jqGrid的使用LINQ到MySQL(的DBLinq)和动态LINQ - ORDERBY不起作用
并试图使其工作。但它给错误= 不能按类型排序System.Object的
请参见code,错误在这里行的。
有人帮我请。
[HttpPost]
公众的ActionResult MyGridData(字符串SIDX,串SORD,诠释页,诠释行)
{
IQueryable的&LT;公司+ GT;库= companyRepository.GetGridCompanies(); INT的PageIndex = Convert.ToInt32(页) - 1;
INT的pageSize =行;
INT总记录= repository.Count();
INT总页数=(int)的Math.Ceiling((浮点)总记录/(浮动)的pageSize); //首先排序数据的IQueryable&LT;门票&GT;不转换了ToList()
IQueryable的&LT;公司+ GT; orderdData =库; 的PropertyInfo的PropertyInfo = typeof运算(公司).GetProperty(SIDX); 如果(的PropertyInfo!= NULL)
{
orderdData =的String.Compare(SORD递减,StringComparison.Ordinal)== 0?
(从库点¯x
排序依据propertyInfo.GetValue(X,NULL)降
选择X):
(从库点¯x
排序依据propertyInfo.GetValue(X,NULL)
选择X);
} 结果//寻呼
IQueryable的&LT;公司+ GT; pagedData = orderdData
.Skip((页大于0页面 - 1:0)*行?)
。取(行); VAR jsonData =新
{
总=总页数,
页,
记录=总记录,
行数=(
从o在pagedData //错误的位置:不能按类型排序System.Object的
新选择
{
I = o.companyID,
电池=新的String [] {o.companyID.ToString(),o.companyName,o.companyCity,o.companyState}
})。ToArray的()
};
返回JSON(jsonData);
}公共类CompanyRepository
{
SandGridDataContext DB =新SandGridDataContext(); //发送公司总数
公众诠释CompanyCount()
{
返回db.Companies.Count();
} 公众的IQueryable&LT;公司+ GT; GetGridCompanies()
{
返回db.Companies;
} 上市公司GetCompany(INT ID)
{
返回db.Companies.SingleOrDefault(D =&GT; d.companyID == ID);
}}
// JS code
jQuery的()。就绪(函数(){
VAR lastSel;
jQuery的(#sandgrid)。jqGrid的({
网址:'/ JQSandbox / MyGridData /',
数据类型:JSON,
MTYPE:'POST',
高度:255,
宽度:640,
colNames:索引,姓名,城市,国家'],
colModel:
{名称:'companyID',索引:companyID',宽度:5},
{名称:'的companyName',索引:'的companyName',宽度:30},
{名称:'companyCity',索引:companyCity',宽度:30},
{名称:'companyState',索引:companyState',宽度:4,排序:假}], 寻呼机:jQuery的('#sandgridp'),
的rowNum:5,
rowList:[5,10,20,50],
sortname:companyID',
排序顺序:递减,
viewrecords:真实,
altRows:真实,
标题:沙盒网格
ondblClickRow:功能(ID){
如果(ID&安培;&安培;!ID == lastSel){
jQuery的('#sandgrid')restoreRow(lastSel)。
lastSel = ID;
警报(你入围+身份证);
}
},
亚格子:真,
subGridUrl:'/ JQSandbox / MySubGridData /',
subGridModel:
{
名称:名称,部,服务时间,监督员]
宽度:[80,20,80,10],
调整:['左','左','左','中心'],
PARAMS:['companyID']
}]
})navGrid('#sandgridp',{编辑:假的,加:假的,德尔:假});
//公司类是LINQ到下面的字段SQL实体。
companyID,
公司名,
companyCity,
companyState
我花了太多的时间来解决这个问题。
字符串&QUOT;通过&QUOT请检查令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 its giving error = Cannot order by type 'System.Object' .
See the line in the code with Error Here.
Someone help me please.
[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
I spent too much time to resolve this. Please check Order LINQ by "string" name.
这篇关于jqGrid的,问题的排序Linq的前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!