When the column name is dynamic, use Linq Order by and send it as a string to a function.

entity-framework linq

Question

I have a Linq (Entity Framework) Query as

function getData(string col_to_sort , bool IsAscending , int pageNo , int pageSize)
{
  context.table_name.Skip(pageNo*pageSize).Take(pageSize).ToArray();
}

What i want is that if i pass the name of the column as a parameter to the function and the order it will sort my query too.

Since my column name will be a string so we might need to convert it to ObjectQuery.

How can i achieve this?

Any help is appreciated

1
5
12/6/2011 10:34:11 AM

Accepted Answer

You can use Dynamic Linq:

string direction = IsAscending ? " ASC" : " DESC";
context.table_name.OrderBy(col_to_sort + direction).Skip(pageNo*pageSize).Take(pageSize).ToArray();
6
12/6/2011 10:37:47 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow