Net core, Ef core and linq. I have table with column status. It holds the values New, In-Progress and closed. When I will query all the rows with New should come first followed by in-progress and closed. Below is my code.
var Requests = await this.RequestRepository.GetAsync(x => x.IsActive == true && x.CreatedBy == LoggedInUser, null, x => x.Country).ConfigureAwait(false);
Below is my GetAsync method
public async Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, params Expression<Func<T, object>>[] includes)
{
IQueryable<T> query = this.dbSet;
foreach (Expression<Func<T, object>> include in includes)
{
query = query.Include(include);
}
if (filter != null)
{
query = query.Where(filter);
}
if (orderBy != null)
{
query = orderBy(query);
}
return await query.ToListAsync().ConfigureAwait(false);
}
So I am expecting all the rows with status New should come first then In-Progress and closed. I am not finding anyway to figure it out. Can someone help me to write this. Any help would be appreciated. Thank you
Your life would probably be a lot easier if you throw this "helper" method away and just use LINQ straight. Look how much more obviously readable it is:
(I skipped the await/async for brevity, not because I'm advocating sync)
Status of course being an enum like:
If your enum is awkwardly NOT in numeric order like this but is instead eg
Then you could order it by descending after you ToString it (you'll notice the names are in reverse alphabet order) or create a mapping using a dictionary that maps eg
d[New] = 0, d[InProgress] = 1 ..., or do an (in-line) if set in the OrderBy.Only the last of these has any real chance of being executed on the server though, and ordering by its name will only work If you don't rename them or add new names that break the ordering.
I do also appreciate the OrderBy/ThenBy given in the other answer and think it's good but I think care should be given to testing whether it executed on the server or the client because it carries out a boolean compare and a lot of db don't use booleans as a data type or if they do they might sort falsy (0) ahead of truey (1)
Might just be then that it sorts all closed and InProg ahead of New because the result of the boolean if evaluated by the server is 0 for InProg/closed and 1 for new. MySql and Postgres would be my top picks for worrying that this would be server evaluated because they do allow boolean type values in contexts other dbs don't.
So, be careful that a) this kind of ordering is evaluating where you expect and want (client/server) and b) if it really is evaluating on the server that the server orders it as you want
Edit, just saw your comment that indicates that Status isn't an enum but some set of string constants??
If your DB values are literally string and status was never an enum then you can just order them by descending