Linq to SQL - Dynamic ordering by column index

1

I'm using a component called datatables.js ( link ) to display my tables and it has the ServerSide strong>, which I call, in Ajax, my list from somewhere. It sends the ordering, meaning by parameters. It sends the Index column to be sorted. My query looks 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 ideas how to do this?

    
asked by anonymous 11.12.2015 / 20:34

1 answer

1

You can use PredicateBuilder , specifically the OrderHelper .

Do something like:

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

Where orders is a OrderInfo array that you can build from dynamic form.

You have an example here too.

    
13.12.2015 / 21:33