【发布时间】:2013-12-27 10:26:29
【问题描述】:
我正在尝试从针对我的 EF Code First DB 运行的 LINQ 选择中返回一组 JSON 格式的数据,并收到错误
Count 必须是 DbConstantExpression 或 DbParameterReferenceExpression。参数名称:计数
但我不确定为什么会得到它,因为我没有在我的 LINQ 查询中使用 COUNT 参数,所以为什么会出现这个错误?
public ActionResult GetData(string sidx, string sord, int page, int rows)
{
try
{
//Get total rows
int RowCount = db.Sections.Count();
string OrderBy = (sidx + " " + sord);
var SectionData = new
{
total = (int)Math.Ceiling((float)RowCount / (float)rows),
page = page,
records = RowCount,
rows = (from s in db.Sections
select new
{
id = s.ID,
cell = new string[] {
SqlFunctions.StringConvert((double)s.ID).Trim(),
s.RouteName,
s.Title
}
.OrderBy(x => sidx)
.Skip(page * rows)
.Take(rows)
}).ToArray()
};
return Json(SectionData, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
return Json(null, JsonRequestBehavior.AllowGet);
}
}
【问题讨论】:
-
您正在使用 RowCount 作为总数....我认为这不是问题,但确实可以计算。
标签: c# linq-to-entities asp.net-mvc-5 entity-framework-6