Dynamic Linq Ordering using System.Linq.Dynamic

Asked

Viewed 168 times

1

I’m trying to sort a list using the library System.Linq.Dynamic, follows the code excerpt:

list = db.TABELA.Where(consulta.Colum + ".Contains(@0) ",     
consulta.filtro).OrderBy("@0 ", "codigo").Take(10).ToList();

but the result, in this case, is always the same, I have tried in some ways (DESC, ASC), it comes to the original ordering of the bank. I’m trying to order in descending order and I can’t. Can anyone help me? Thanks from now!

  • Only with this it is difficult to help. It needs context. In a general way this n]ao is to work even.

1 answer

1

The OrderBy in your query is generating an ordination of the word code and not from the countryside codigo as it is in your expression Linq Dynamic (OrderBy("@0 ", "codigo")), in SQL below it is very clear:

SELECT
    [Project1].*    
    FROM ( SELECT
        N'Codigo' AS [C1],        
        FROM [dbo].[TABELA] AS [Extent1]
    )  AS [Project1]
    ORDER BY [Project1].[C1] ASC

So to function properly, it should be:

OrderBy("Codigo ASC")

A very useful tip is to check by Log of the Entity Framework what is being generated:

using (MyDbContext db = new MyDbContext())
{
     db.Database.Log = c => Console.WriteLine(c);
     //...
}

In this case generates direct on the console which SQL were generated and with that you debug knowing why of the inconsistencies.

Browser other questions tagged

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