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:

2 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:

    Thanks
    so good

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>