在关注https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/crud的学习包时,我想知道在以下代码中进行了哪些更改,以便以可排序的方式提供课程详细信息表(如在{ {3}})。我可以使用一个提供所需排序方式的参数来调用Details方法,但是如何让var学生使用OrderBy& OrderByDescending因为Visual Studio抱怨它本质上不是IQueryable:
$script
虽然索引页面的代码(如下所示)适用于var学生:
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Students
.Include(s => s.Enrollments)
.ThenInclude(e => e.Course)
.AsNoTracking()
.SingleOrDefaultAsync(m => m.ID == id);
if (student == null)
{
return NotFound();
}
return View(student);
}
提前致谢。
更新
我想,我不是很好地提出这个问题。让我们考虑另一个例子:
public async Task<IActionResult> Index(string sortOrder)
{
ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";
var students = from s in _context.Students
select s;
switch (sortOrder)
{
case "name_desc":
students = students.OrderByDescending(s => s.LastName);
break;
case "Date":
students = students.OrderBy(s => s.EnrollmentDate);
break;
case "date_desc":
students = students.OrderByDescending(s => s.EnrollmentDate);
break;
default:
students = students.OrderBy(s => s.LastName);
break;
}
return View(await students.AsNoTracking().ToListAsync());
}
换句话说,只有余额表以排序的方式反映余额。
再次感谢。