Mat*_*ete 2 c# linq-to-entities entity-framework-6 asp.net-mvc-5
我试图从针对我的EF Code First DB运行的LINQ选择中返回JSON格式中的一组数据,并且我收到错误
Count必须是DbConstantExpression或DbParameterReferenceExpression.参数名称:count
但我不知道为什么我得到它,因为我没有COUNT在我的LINQ查询中使用参数,所以为什么我收到此错误?
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Gra*_*ICA 17
我不太熟悉EF,但我确实遇到过一个旧的msdn帖子,有人报告了同样的错误.他们在里面执行计算.Skip()并修复它们,他们分别执行计算,并在结果中使用了他们的LINQ语句.
page * rows首先尝试计算,然后在LINQ语句中使用该结果:
var skipAmount = page * rows;
var SectionData = new
{
...
...
rows = (from s in db.Sections
select new
{
...
...
.OrderBy(x => sidx)
.Skip(skipAmount)
.Take(rows)
Run Code Online (Sandbox Code Playgroud)