0

I try to place results of below link in a paged list:

LINQ to SQL and a running total on ordered results

I change the code slightly :

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));

but 'remain' results don't start with 1. it start with 8 for example if 'Docs' records count is 7

Why ??? !!!

if I return View(moeen) results is Ok

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
sde.mahdi
  • 41
  • 5

1 Answers1

0

I find problem

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));

before list moeen by .ToList(), every use of moeen cause to side effect on running_total.

See this : http://www.blackwasp.co.uk/LinqRunningTotal.aspx

if you change the value of the variable that has been closed over before executing the query, the results will be affected.

sde.mahdi
  • 41
  • 5