1

I have a table which has hundreds of columns (like FirstName and LastName).

All this data is displayed as a GridView with listing. I've made use of the SortCommand event which has the e.SortExpression, but I can't fathom how to use it in an OrderBy function since after inputting a lambda expression. IntelliSense shows me all the hundreds of columns instead of the one I need - which I only have at run-time.

How do I use the Func<TSource, TKey> selector the OrderBy functions expects from me to let it know that the string e.SortExpression is the column that needs to be sorted?

Example of what I essentially mean:

private void dataGrid_SortCommand(object source, DataGridSortCommandEventArgs e)
{
    var users = new UsersEntities().UsersTable.OrderBy(x => "x."+e.SortExpression);
    FillDataGrid(users); 
}

Update: I've come to understand that the way in which I'm looking to accomplish this is using Expression trees, and I'm hoping someone can provide the exact solution.

Update 2: I've found the solution here. Turns out my question was a duplicate and to add to that, badly worded. Thanks to everyone.

And I've also just noticed that ken's offered pretty damning evidence that I should use dynamic linq. I'll consider doing so in the future, but I couldn't help but notice how fun it is to experiment with Expression trees rather than rely on libraries to do them for me. At this stage, as I'm still new to Expression trees this is a perfect opportunity to utilize them.

Community
  • 1
  • 1
Agon Noga
  • 563
  • 1
  • 9
  • 17

2 Answers2

1

You can use dynamic linq That would allow you to do an OrderBy based on a string.

Edit

See my comment. Using dynamic linq, your solution could be as easy as:

var users = new UsersEntities().UsersTable.OrderBy(e.SortExpression);
qxn
  • 17,162
  • 3
  • 49
  • 72
  • Any chance of being able to stick with pure Ado.Net? I have an unexplainable distaste for SQL as a language. – Agon Noga Apr 11 '13 at 13:28
  • @Agon - I'm confused... are you using an ORM (LINQ-to-SQL, EntityFramework)? It looks like you are. Dynamic linq works with ORMs. – qxn Apr 11 '13 at 14:04
  • I'm using purely an Entity (entity framwork). And prefer to stick to C# as much as humanely possible. – Agon Noga Apr 11 '13 at 14:12
  • @Agon - Dynamic Linq and Entity Framework work well together. Dynamic Linq != dynamic SQL. It is a library that builds expressions from strings, and those expressions can be used in IQueryable Where and OrderBy methods. – qxn Apr 11 '13 at 15:13
  • Thank you, ken, for explaining. I've chosen your reply as the answer. – Agon Noga Apr 11 '13 at 15:24
0

You can dynamically create LINQ expressions easily as long as only logical ANDs are involved in where clauses. Simply repeat the Where clause!

// Filter
var query = new UsersEntities().UsersTable.Select(x => x);
if (!String.IsNullOrEmpty(nameFilter) {
    query = query.Where(x => x.Name == nameFilter);
}
if (!String.IsNullOrEmpty(zipFilter) {
    query = query.Where(x => x.Zip == zipFilter);
}

// Sorting
switch (sortField)
{
    case "Name":
        query = query.OrderBy(x => x.Name);
        break;
    case "Zip":
        query = query.OrderBy(x => x.Zip);
        break;
}

You could also create an expression tree dynamically, but this is not very obvious.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Thanks for the suggestion, but I don't think it's practical to have hundreds of redundant lines, the very concept of programming kinda loses its purpose. I've been trying for the past hour now to get this done using expressions, but I'm completely new to the topic of Expression trees (only found out about them after creating this question) so I'm having a hard time. But this method is basically exactly what I'm looking for. – Agon Noga Apr 11 '13 at 14:39
  • See: [How to: Use Expression Trees to Build Dynamic Queries (C# and Visual Basic)](http://msdn.microsoft.com/en-us/library/vstudio/bb882637.aspx). – Olivier Jacot-Descombes Apr 11 '13 at 14:48
  • Thanks, but I've been going hard at that exact page (and a dozen more) for that whole hour now, and still unsuccessful. – Agon Noga Apr 11 '13 at 14:55