I've checked other posts on this, but not seeing anything similar enough to my code to clue me in enough to fix it. Please advise.
I have a report on my razor page app. Here is the .cs for it:
{
public class IndexModel : PageModel
{
private readonly ApplicationDbContext _context;
public IndexModel(ApplicationDbContext context)
{
_context = context;
}
public Attendance Attendance { get; set; }
public Client Client { get; set; }
public string NameSort { get; set; }
public string CurrentSort { get; set; }
public IEnumerable<Panel> DisplayPanelData { get; set; }
public IEnumerable<Status> DisplayStatusData { get; set; }
public PaginatedList<ReportModel> Registrants { get; set; } = default!;
public async Task OnGet(string sortOrder, string panelAssigned, string attendanceStatus, int? pageIndex)
{
await _context.Panel.Select(a => a.PanelName).ToListAsync();
DisplayPanelData = await _context.Panel.ToListAsync();
await _context.Status.Select(a => a.StatusDesc).ToListAsync();
DisplayStatusData = await _context.Status.ToListAsync();
IQueryable<ReportModel> Registrants = (IQueryable<ReportModel>)await (from x in _context.Attendance
join y in _context.Client on x.ClientId equals y.Id
where x.PanelAssigned == panelAssigned && x.Status == attendanceStatus
select new ReportModel
{
ClientId = x.ClientId.ToString(),
ClientLastName = y.ClientLastName,
ClientFirstName = y.ClientFirstName,
ClientMI = y.ClientMI,
PanelCost = x.PanelCost,
AmountPaid = x.AmountPaid,
AmountOutstanding = x.AmountOutstanding,
Status = x.Status,
}).ToListAsync();
CurrentSort = sortOrder;
NameSort = string.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
switch (sortOrder)
{
case "name_desc":
Registrants = Registrants.OrderByDescending(x => x.ClientLastName).ThenByDescending(x => x.ClientFirstName);
break;
default:
Registrants = Registrants.OrderBy(x => x.ClientLastName).ThenBy(x => x.ClientFirstName);
break;
}
int resultsPerPage = 100;
Registrants = (IQueryable<ReportModel>)await PaginatedList<ReportModel>.CreateAsync(
(Registrants, pageIndex ?? 1, resultsPerPage);
ViewData["panelparameter"] = $"Panel Selected: {panelAssigned}";
ViewData["statusparameter"] = $"Status Selected: {attendanceStatus}";
}
}
}
Here is the full error:
InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List1[SampleApp.Model.ReportModel]' to type 'System.Linq.IQueryable
1[SampleApp.Model.ReportModel]'.
When I run the app and navigate to the report page link, it errors out with this line highlighted in red:
IQueryable Registrants = (IQueryable)await (from x in _context.Attendance
Any direction would be much appreciated. Spinning my wheels. Thank you!!
ToListAsync()
would query db at once,if you don't want to query db at once just want IQueryable<ReportModel>
don't call it
Also not all types could be casted to another directly ,that's the reason for the error,you could check this document related
modify:
IQueryable<ReportModel> Registrants = (IQueryable<ReportModel>)await (from x in _context.Attendance
join y in _context.Client on x.ClientId equals y.Id
where x.PanelAssigned == panelAssigned && x.Status == attendanceStatus
select new ReportModel
{
ClientId = x.ClientId.ToString(),
ClientLastName = y.ClientLastName,
ClientFirstName = y.ClientFirstName,
ClientMI = y.ClientMI,
PanelCost = x.PanelCost,
AmountPaid = x.AmountPaid,
AmountOutstanding = x.AmountOutstanding,
Status = x.Status,
}).ToListAsync();
To
IQueryable<ReportModel> Registrants = from x in _context.Attendance
join y in _context.Client on x.ClientId equals y.Id
where x.PanelAssigned == panelAssigned && x.Status == attendanceStatus
select new ReportModel
{
ClientId = x.ClientId.ToString(),
ClientLastName = y.ClientLastName,
ClientFirstName = y.ClientFirstName,
ClientMI = y.ClientMI,
PanelCost = x.PanelCost,
AmountPaid = x.AmountPaid,
AmountOutstanding = x.AmountOutstanding,
Status = x.Status,
};