当我尝试在Asp.Net MVC中创建服务器端数据表时,我遇到了问题 代码如下所示:
[HttpPost]
public ActionResult CustomerList()
{
var draw = Request.Form.GetValues("draw").FirstOrDefault();
var start = Request.Form.GetValues("start").FirstOrDefault();
var length = Request.Form.GetValues("length").FirstOrDefault();
var sortColumn = Request.Form.GetValues("columns[" +
Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();
var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();
int pageSize = length != null ? Convert.ToInt32(length) : 0;
int skip = start != null ? Convert.ToInt32(start) : 0;
int totalRecords = 0;
using (ApplicationDbContext db = new ApplicationDbContext())
{
var resultData = (from cust in db.Customers
//orderby sortColumn
select cust);
//Sorting
if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
{
resultData = resultData.OrderBy(sortColumn + " " + sortColumnDir); // Here is failing (OrderBy)
}
totalRecords = resultData.Count();
var data = resultData.Skip(skip).Take(pageSize).ToList();
return Json(new
{
draw = draw,
recordsFiltered = totalRecords,
recordsTotal = totalRecords,
data = data
}, JsonRequestBehavior.AllowGet);
}
}
问题出在这一行:
resultData = resultData.OrderBy(sortColumn + " " + sortColumnDir);
错误:
CS0411 The type arguments for method 'Queryable.OrderBy<TSource, TKey>(IQueryable<TSource>, Expression<Func<TSource, TKey>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly
先谢谢你们!
答案 0 :(得分:0)
我解决了..我添加了命名空间:using System.Linq.Dynamic;并在问题行中的is中添加了Tolist():