Entity Framework Core - Sorting

c# entity-framework-core sorting

Question

I'm trying to sort my data set like this:

public async Task<ICollection<Article>> SortArticles(string sortOrder)
{
    IQueryable<Article> articles = GetAll();
    switch (sortOrder)
    {
        case "name_desc": 
            articles.OrderByDescending(s => s.Name).AsQueryable();
        break;
        case "date":
            articles = articles.OrderBy(s => s.DateCreated).AsQueryable();
        case "date_desc":
            articles = articles.OrderByDescending(s => s.DateCreated).AsQueryable();
        break;
        default:
             articles.OrderBy(s => s.Name).AsQueryable();
        break;
    }
    var result = await articles.ToListAsync();
    return result;
}

However, my result is always the ordered same way.

What I'm doing wrong?

1
0
8/23/2018 9:59:06 PM

Popular Answer

Added the missing break and you are also missing the assignment of the sort.

public async Task<ICollection<Article>> SortArticles(string sortOrder)
{
    IQueryable<Article> articles = GetAll();
    switch (sortOrder)
    {
        case "name_desc": 
            articles = articles.OrderByDescending(s => s.Name).AsQueryable();           
        break;
        case "date":
            articles = articles.OrderBy(s => s.DateCreated).AsQueryable();
            break;
        case "date_desc":
            articles = articles.OrderByDescending(s => s.DateCreated).AsQueryable();
            break;
        default:
             articles = articles.OrderBy(s => s.Name).AsQueryable();
            break;
    }
    var result = await articles.ToListAsync();
    return result;
}
0
8/23/2018 10:20:31 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow