OrderBy a string in Entity Framework

I’ve just started using Entity Framework, and I’ve found that it really helps speed up development time when dealing with queries where performance isn’t a huge issue.

I added to an existing project which needed to keep the EF classes isolated in the data layer. This made it difficult to pass in a sort order without having a massive switch statement to deal with each of the options.

I solved this by building an OrderBy extension method that takes a string, and builds the sort expression using reflection. This means you can sort without having to know the actual type of what you’re ordering by.

Here’s the Gist:

5 Responses to “ “OrderBy a string in Entity Framework”

  1. Steve says:

    This is good stuff. Is it possible with this to do an order by that is case insensitive?

  2. Samer says:

    so good

  3. manit says:

    this is great code

    Thank you.

  4. You made my day bro! I have been implementing a entity-independent system and this works like a charm!

  5. drumhacker says:

    Great job, thanks for the help. Will pass this on.