I have this generic method that I'm trying to make, and in order to achieve that I want to pass an IOrderedQueryable as a parameter.
public async Task<IEnumerable<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> filter, IOrderedQueryable orderBy){
IQueryable<TEntity> query = dbSet;
query = query.Where(filter);
query = query.OrderBy(orderBy); //This is what I want to do
return await query.ToListAsync();
}
I want to pass an IOrderedQueryable from outside in order to be able to make the function depending on a string and be able to look at the properties of my object (without reflection), for one and each of my objects.
public static Func<IQueryable<MyObject>, string, IOrderedQueryable<Entity>> OrderBy = (query, orderBy) =>
{
return orderBy switch
{
"Id" => query.OrderBy(x => x.Id),
"Prop1" => query.OrderBy(x => x.Prop1),
"Prop2" => query.OrderBy(x => Prop2),
_ => query.OrderBy(x => x.Id)
};
};
I don't know if I change the order if it would have any impact.
query = orderBy;
query = query.Where(filter);
Or I don't know if I can do a Concat, or if that would make me lose my where clause
query = query.Concat(orderBy);