大廈從這個問題進行排序匿名類型:Trying to concatenate columns in LINQ如何在不命名的列
現在我要進行排序:
var orderByIndexVal = Convert.ToInt32(Request["iSortCol_0"]);
var direction = Request["sSortDir_0"];
var gridInfo =
from leader in db.SchoolLdrAdminAccesses
join emp in db.Employees
on leader.ID equals emp.ID
select new List<string> { leader.ID, emp.FirstName + " " + emp.LastName };
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = gridInfo.Count(),
iTotalDisplayRecords = gridInfo.Count(),
aaData = gridInfo.ToArray()
},
JsonRequestBehavior.AllowGet);
閱讀這篇文章後: LINQ sorting anonymous types?
我的問題是,我沒有任何名字在我的gridInfo
什麼到目前爲止,我已經試過:
if (direction == "asc")
{
gridInfo = gridInfo.OrderBy(gi => gi[orderByIndexVal]);
}
else
{
gridInfo = gridInfo.OrderByDescending(gi => gi[orderByIndexVal]);
}
但我發現了以下錯誤:
LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression.
在下面的代碼塊:
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = gridInfo.Count(),
iTotalDisplayRecords = gridInfo.Count(),
aaData = gridInfo.ToArray()
},
JsonRequestBehavior.AllowGet);
不知道我明白這個問題,但'gridInfo = gridInfo.OrderBy(...)'看起來應該這樣做。你嘗試過嗎? – Jon
@Jon我更新了我的問題,告訴你我做了什麼以及我作爲錯誤得到了什麼。 – webdad3