Linq to SQL - Dynamic sort by column index

Asked

Viewed 92 times

1

I’m using a component called datatables.js (https://www.datatables.net/) to display my tables and it owns the property Serverside, that I call, in Ajax, my list from somewhere. It sends the ordering, felt by parameters. It sends the Index of the column to be ordered. My query is like this:

var test1 = (from x in Context.MyTable
    where (x.codigo_empresa == User.EnterpriseId
        && (filtro.DataInicial == DateTime.MinValue || x.Vencimento >= filtro.DataInicial)
        && (filtro.DataFinal == DateTime.MinValue || x.Vencimento <= filtro.DataFinal)            
        && (filtro.Status == 0 || x.Status == filtro.Status))
    select (new
    {
        Status = (EnumContas.Status)x.Status,
        Vencimento = x.Vencimento ?? DateTime.MinValue,                                  
        Valor = x.Valor,                 
        id = x.Id,
        IdConta = x.IdConta
    }));

I need to sort it according to the column index that comes by parameters (assuming my select is in the order in which the columns will be displayed).

Any idea how to do that?

1 answer

1


You can use the Predicatebuilder, more specifically the Orderhelper.

Do something like:

var ordenado = OrderHelper.GetOrderedQueryable<Entidade>(query, orders);

Where orders is an array Orderly that you can build dynamically.

Have an example here also.

  • Thank you very much. I didn’t exactly use the code you sent me, but it helped me a lot in how to do.

Browser other questions tagged

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