我尝试将以下链接的结果放在分页列表中:
LINQ to SQL and a running total on ordered results
我稍微更改了代码:
var userDocs = db.Docs.AsQueryable();
Int64 running_total = 0;
var moeen = userDocs.ToList()
.OrderBy(a => a.DocDate)
.Select(a =>
{
running_total += 1;
return new MoeenViewModel
{
Doc = a,
remain = running_total
};
}
);
return View(moeen.ToPagedList(1, 15));
但是“保留”结果不以1开头。例如,如果“文档”记录数为7,则以8开头
为什么??? !!!
如果我返回View(moeen)结果是OK
答案 0 :(得分:0)
我发现问题
var userDocs = db.Docs.AsQueryable();
Int64 running_total = 0;
var moeen = userDocs.ToList()
.OrderBy(a => a.DocDate)
.Select(a =>
{
running_total += 1;
return new MoeenViewModel
{
Doc = a,
remain = running_total
};
}
);
moeen = moeen.ToList();
return View(moeen.ToPagedList(1, 15));
在列表moeen by .ToList()之前,每次使用moeen都会对running_total造成副作用。
看到这个: http://www.blackwasp.co.uk/LinqRunningTotal.aspx
如果在执行查询之前更改已关闭的变量的值,则结果将受到影响。