我已经检查过其他关于此问题的帖子,但没有看到任何与我的代码足够相似的内容来提示我足够的信息来修复它。请指教。
我的剃刀页面应用程序有一份报告。这是它的 .cs:
{
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}";
}
}
}
这是完整的错误:
InvalidCastException:无法转换类型为“System.Collections.Generic.List
1[SampleApp.Model.ReportModel]' to type 'System.Linq.IQueryable
1[SampleApp.Model.ReportModel]”的对象。
当我运行应用程序并导航到报告页面链接时,出现错误并以红色突出显示此行:
IQueryable 注册者 = (IQueryable)await(来自 _context.Attendance 中的 x
任何方向将不胜感激。转动我的轮子。谢谢!!
您需要在任何 IEnumerable(如
.AsQueryable()
)上调用 List
来获取其可查询项。类型转换不起作用。