最简单的方法是使用 LINQ to DataSet 扩展。首先需要使用 LINQ to DataSet 从 DataTable 创建一个通用列表(SearchSerialResults 在这种情况下只是一个 DTO)。
var resultItems = (from DataRow dr in _returnedData.AsEnumerable() select new SearchSerialResults {
ContractLineItem = (int) dr["fldContractLineItemID"],
SearchItem = (string) dr["Search Item"],
Customer = (string) dr["Customer"],
DeviceFound = (string) dr["Device Found"],
Country = (string) dr["Country"],
City = (string) dr["City"],
ContractNumber = (string) dr["Contract Number"],
QuoteNumber = (string) dr["Quote Number"],
BeginDate = (string) dr["Begin Date"],
EndDate = (string) dr["End Date"]
}).ToList();
_returnedData 在这种情况下是 DataTable。第 2 步是进行转换。在这种情况下,我为 jqGrid 返回一个 Json 对象。
var jsonObject = new {
total = totalPages,
pageSize,
records = totalRecords,
rows = (from SearchSerialResults item in resultItems select new {
id = item.ContractLineItem,
cell = new [] {
item.ContractLineItem.ToString(),
item.SearchItem,
item.DeviceFound,
item.Customer,
item.ContractNumber,
item.QuoteNumber,
item.Country,
item.City,
item.BeginDate,
item.EndDate,
""
}
}).ToArray()
};
return Json(jsonObject) // for MVC