.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

  • OpenSilver 3.1 Unveils Drag-and-Drop XAML Designer for VS Code

    Claiming an industry first, Userware announced a drag-and-drop XAML designer for use in VS Code, coming with OpenSilver 3.1, the latest iteration of the open-source implementation of Microsoft's long-deprecated and oft-mourned rich client development platform, Silverlight.

  • Visual Studio Dev Vexed by Copilot's Obtuse 'Responsible AI' Refusal

    No matter what, some systems simply won't divulge exactly what in the current conversation caused them to balk, simply pointing to vague policies and generic guidance.

  • Build Your First AI Applications with Local AI

    "AI right now feels like a vast space which can be hard to jump into," says Craig Loewen, a senior product manager at Microsoft who is helping devs unsure about making that first daunting leap.

  • On Blazor Component Reusability - From Day 0

    "We want to try to design from Day One, even Day Zero, with reusability in mind," says Blazor expert Allen Conway in imparting his expertise to an audience of hundreds in an online tech event on Tuesday.

  • Decision Tree Regression from Scratch Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of decision tree regression using the C# language. Unlike most implementations, this one does not use recursion or pointers, which makes the code easy to understand and modify.

Subscribe on YouTube