0

While following the learning package at https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/crud, I want to know what changes to be made in the following code so as to have the Course Details table be available in the sortable manner (like on the Index page at https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page). I can call the Details method with an argument delivering the sort manner required but how to have the var student use the OrderBy & OrderByDescending since the same are complained by the Visual Studio to be not IQueryable in nature:

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

While code for the Index page, like the following, works great for the var students:

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

Thanks in advance.

Update

I guess, I am not presenting the problem well enough. Let's consider another example:

    public async Task<IActionResult> Details(string _AccId)
    {
        if (_AccId == null)
        {
            return NotFound();
        }
        var accs = await _context.Accounts
            .Include(Cust => Cust.Customers) //To reflect the Name of the Customer to whom the account belongs
            .Include(Bal => Bal.Balances).OrderByDescending(Balances.Report_Date) //For relevant Sub-Table on Accounts' detail page to show the balances on in shape of Report_Date (but in a descending sorted manner)
            .SingleOrDefaultAsync(m => m.SrcSys == _SrcSys && m.CustId == _CustId && m.AccId == _AccId);
        return View(accs);
    }

In other words only the Balances Table reflecting balances in a manner sorted.

Thanks again.

InAction
  • 45
  • 6
  • See full source code with corrections : https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-mvc/intro/samples/cu-final – jdweng May 14 '17 at 21:11
  • As stated the code only reflects the sorting feature added to the Index view and no example for a table on the Edit. How would one sort something like: `var student = await _context.Students .Include(s => s.Enrollments) .ThenInclude(e => e.Course) .AsNoTracking() .SingleOrDefaultAsync(m => m.ID == id);` – InAction May 15 '17 at 06:56
  • See first answer at : http://stackoverflow.com/questions/1578778/using-iqueryable-with-linq – jdweng May 15 '17 at 07:56

0 Answers0