How to define the order of a list dynamically using Linq and C#

Asked

Viewed 171 times

0

Hello, I am creating a method so that I can return a list of people where, I pass the column that should be ordered and whether this is increasing or decreasing. Thus:

public async Task<List<Pessoa>> getAsync() => await getAsync("id", false);
public async Task<List<Pessoa>> getAsync(string order) => await getAsync(order, false);
public async Task<List<Pessoa>> getAsync(string order, bool desc)
{
    using (var context = Context())
    {
        try
        {
            var list = context.ContaBancaria.AsNoTracking();

            return desc ? 
                await list.OrderByDescending(x => x.GetType().GetProperty(order))
                    .ToListAsync() :
                await list.OrderBy(x => x.GetType().GetProperty(order))
                    .ToListAsync();

            }
        catch
        {
            return null;
        }
    }
}

My question is whether there is any way for me to decide the ordering of the list (i.e., or to use the method OrderBy or the OrderByDescending in a more "dynamic" way than this?

NOTE: when I say dynamic, I mean in code economy. I thought about creating a method OrderBy to accept one more field, this being the reference of increasing or decreasing and, in this method to make the treatment. Thus its use would be something of this type:

public async Task<List<Pessoa>> getAsync() => await getAsync("id", false);
public async Task<List<Pessoa>> getAsync(string order) => await getAsync(order, false);
public async Task<List<Pessoa>> getAsync(string order, bool desc)
{
    using (var context = Context())
    {
        try
        {
            var list = context.ContaBancaria.AsNoTracking();

            return await list
                .OrderBy(x => x.GetType().GetProperty(order), desc)
                .ToListAsync();
        catch
        {
            return null;
        }
    }
}
  • How I always talk and nobody listens, depends on. If you will do it in your mind, that is, use a IEnumerable beauty a solution like this, whether it will do in the database, ie a IQueryable is different, you need to do a translation for the provider and this can create a horrible and not performatic code. mainly using reflection (which should already be avoided even in memory, is absurdly slow).

  • I don’t understand... honestly. The question I raised is that, I have 2 existing methods in Linq and I want to turn into only 1, where depending on the parameter you pass (true/false) it will call one or the other. I do not see why this reasoning being that I will not remake the EF

  • See https://answall.com/a/319077/101, https://answall.com/q/182176/101, https://answall.com/q/125811/101. If you do not understand, ask a specific question. to tell the truth I think that nobody should use LINQ without understanding the things, are doing horrible and slow things that hurt.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.