下面的操作方法,如本文中所描述的JSON數據返回給我的jqGrid:
using-jquery-grid-with-asp.net-mvcLINQ查詢爲了幫助
我的問題是在此基礎上的LINQ查詢:
var data = (
from job in jobs
select new
{
id = job.Id,
cell = new List<string>() { job.Industry, job.OccupationName }
}).ToArray();
爲什麼細胞列表條目未按預期排序? (工業後跟OccupationName):
我想到的是:
{"total":1400,"page":1,"records":7000,
"rows":[
{"id":"1","cell":{"Industry":"IT","OccupationName":"Administrator"}},
{"id":"2","cell":{"Industry":"IT","OccupationName":"Developer"}},
{"id":"4","cell":{"Industry":"IT","OccupationName":"Programmer"}}
]}
什麼,我得到的是:
{"total":1400,"page":1,"records":7000,
"rows":[
{"id":"1","cell":{"Industry":"IT","OccupationName":"Administrator"}},
{"id":"2","cell":{"Industry":"Developer","OccupationName":"IT"}},
{"id":"4","cell":{"Industry":"IT","OccupationName":"Programmer"}}
]}
感謝您的任何建議!
爲了清楚起見,整個動作方法:
public ActionResult OccupationList(string sidx, string sord, int page, int rows)
{
using (var context = Service.EF.GetContext())
{
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
int totalRecords = context.sOccupations.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords/(float)pageSize);
sidx = "it.[" + sidx + "]";
var jobs = context.sOccupations
.OrderBy(sidx + " " + sord)
.Skip(pageIndex * pageSize)
.Take(pageSize);
var data = (
from job in jobs
select new
{
id = job.Id,
cell = new List<string>() { job.Industry, job.OccupationName }
}).ToArray();
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = data
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
}
我目前的解決方案是一個經典的foreach陳述書 但我仍然不知道如何表達這種LINQ查詢
IList<JsonRow> data = new List<JsonRow>();
JsonRow jsonRow;
foreach (var item in jobs)
{
jsonRow = new JsonRow();
jsonRow.id = item.Id;
jsonRow.cell = new string[2] { item.Industry, item.OccupationName };
data.Add(jsonRow);
}
class JsonRow
{
public Guid id { get; set; }
public string[] cell { get; set; }
}
您確定你的信息不對? – msarchet 2011-05-23 14:44:14
是的,數據是正確的:) – 2011-05-23 15:24:12