Reputation: 183
In my ASP.NET 4.8 web app, the following works; I'm using PagedList
for pagination:
private AppDbContext db = new AppDbContext();
private IQueryable<Border> Broders;
public ViewResult Index(int? page)
{
Broders = from s in db.Broders select s;
if (SearchColumn == "T1")
{
Borders = Broders.Where(s => s.TagT1.ToString().Contains(searchString));
}
else if (SearchColumn == "T2")
{
Borders = Broders.Where(s => s.TagT2.ToString().Contains(searchString));
}
switch (sortOrder)
{
case "status_desc":
Borders = Borders.OrderByDescending(s => s.Status);
break;
case "status_desc":
Borders = Borders.OrderByDescending(s => s.Status);
break;
default:
Borders = Borders.OrderByDescending(s => s.CreatedOn);
break;
}
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(Borders.ToPagedList(pageNumber, pageSize));
}
I do this in an ASP.NET Core 8 MVC app using X.PagedList
for pagination:
private readonly AppDbContext _context;
private IQueryable<Broder> Borders;
public async Task<IActionResult> Index(int? page)
{
var Borders = await _context.Borders.ToListAsync();
if (SearchColumn == "T1")
{
Borders = (List<Initiative>)Broders.Where(s => s.TagT1.ToString().Contains(searchString));
}
else if (SearchColumn == "T2")
{
Borders = (List<Initiative>)Broders.Where(s => s.TagT2.ToString().Contains(searchString));
}
switch (sortOrder)
{
case "status_desc":
Borders = (List<Initiative>)Borders.OrderByDescending(s => s.Status);
break;
case "status_desc":
Borders = (List<Initiative>)Borders.OrderByDescending(s => s.Status);
break;
default:
Borders = (List<Initiative>)Borders.OrderByDescending(s => s.CreatedOn);
break;
}
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(Borders.ToPagedList(pageNumber, pageSize));
}
This ASP.NET Core code throws an error
InvalidCastException: Unable to cast object of type 'System.Linq.OrderedEnumerable
2[Map.Models.Border,System.String]' to type 'System.Collections.Generic.List
1[Map.Models.Border]'
at line
Borders = (List<Initiative>)Borders.OrderByDescending(s => s.CreatedOn);
I'm new to ASP.NET Core, this is my first application. Can someone tell me what mistake I am making in my ASP.NET core method?
Upvotes: -1
Views: 165
Reputation: 51220
From this line:
var Borders = await _context.Borders.ToListAsync();
you are declaring Borders
which is a List<Border>
type.
Changes:
Declare the Borders
as IQueryable<Border>
with .AsQueryable()
.
Remove casting to List<Initiative>
which seems unnecessary.
Don't need to create variable with global scope: private IQueryable<Broder> Borders;
public async Task<IActionResult> Index(int? page)
{
IQueryable<Broder> Borders = _context.Borders.AsQueryable();
if (SearchColumn == "T1")
{
Borders = Borders.Where(s => s.TagT1.ToString().Contains(searchString));
}
else if (SearchColumn == "T2")
{
Borders = Borders.Where(s => s.TagT2.ToString().Contains(searchString));
}
switch (sortOrder)
{
case "status_desc":
Borders = Borders.OrderByDescending(s => s.Status);
break;
case "status_desc":
Borders = Borders.OrderByDescending(s => s.Status);
break;
default:
Borders = Borders.OrderByDescending(s => s.CreatedOn);
break;
}
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(Borders.ToPagedList(pageNumber, pageSize));
}
Upvotes: -1
Reputation: 152566
In your second code block you use var Borders = ...
whcih defines a new variable of a different type (implied from the return of ToList
and hides the page field. So just removing the var
would be a start to get them in sync, but it may require other changes as well.
For example, you may also need to remove the ToList
since a list cannot be assigned to an IQueryable
variable. It will also stop loading the entire Borders
table in memory only to apply filters after that.
Upvotes: 0