.NET Tips and Tricks

Blog archive

Sorting with LINQ, Dynamically

In the bad old days (before LINQ), I used to concatenate together SQL statements. That left me open to SQL injection attacks, of course, but it was so convenient: I could dynamically put together whatever SQL I needed at runtime. With LINQ, however, not so much (though I understand that there is a version of Dynamic LINQ out there). Even with ordinary LINQ, though, you can select which properties in an object you want to use at runtime; at least when you're sorting (and, probably, elsewhere but I haven't tried it yet).

Recently, I needed to support a grid for a client who wanted to be able to sort on any column. I was setting the grid's DataSource to a collection of objects. While this simplified getting the objects displayed, it also turned off the grid's automatic sorting capability. To get around that, I added some code to the grid's ColumnHeaderMouseClick event to sort the grid. What I didn't want to do was write a separate method to sort on each grid column (especially because this client kept changing his/hers/its mind about what columns should or shouldn't be in the grid).

Fortunately, each grid column held the name of the property that the column was responsible for displaying from the objects in the grid's DataSource. Given a property's name, I retrieved the PropertyInfo for that property and used its GetValue method in my LINQ code.

First, I retrieve a sample object from the collection of objects (Customers, in this case):

Dim cust As Customer
cust = Customers.First

Then I retrieve the PropertyInfo object for the property whose name is in the PropertyName string variable:

Dim prop As PropertyInfo
prop = cust.GetType.GetProperties.First(
           Function(pr) pr.Name = propertyName)

Now I use the PropertyInfo object's GetValue method in my LINQ statement to sort my list:

Customers = Customers.OrderBy(
      Function(c) prop.GetValue(c, Nothing)).ToList

I'm looking forward to finding out how many places I can use this technique.

Posted by Peter Vogel on 10/07/2013


comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube