LINQ Cookbook, Recipe 9: Dynamic Sort Order (Doug Rothaus)
· Visual Studio 2008 (Beta2 or Higher)
You can use the Order By clause to easily sort the results of a LINQ query in Visual Basic. A common requirement for sorted data, however, is that the user be able to choose the fields that the results are sorted by. The Order By clause requires that you specify a static list of sort fields and sort directions, so how do you specify a sort order when you don’t know which fields the user has chosen until run time? The solution is to use the OrderBy or OrderByDescending query methods of the Queryable or Enumerable class.
Here’s how it works: The OrderBy or OrderByDescending methods can be applied to any LINQ query. The input to the OrderBy or OrderByDescending methods is either a Lambda expression, or the address of a custom function. The Lambda expression or custom function evaluate an instance of the range variable for the current query scope and return a value to be used when sorting. For example, if the range variable is a Customer object that has FirstName and LastName properties, you can create a function called GetSortValue that takes, as input, a Customer object and returns the value for either the FirstName or LastName property value, based on which field the user selected to sort by.
This recipe shows how to create a custom function to provide values to be used when sorting a LINQ query.
· Create a new Windows Forms Application in Visual Basic. Set the Size of the default Form, Form1, to 800, 600.
· From the Toolbox, drag a WebBrowser control onto the form. The name of the WebBrowser control will default to WebBrowser1. In the WebBrowser Tasks panel, select Undock in parent container and resize the WebBrowser control to make room for two Label controls on the form.
· From the Toolbox, drag a Label control onto the form. Set the Text property of the Label control to Date. The name of the Label control will default to Label1.
· From the Toolbox, drag another Label control onto the form. Set the Text property of the Label control to Title. The name of the Label control will default to Label2.
· Right-click the Form and select View Code.
At this point, you will add a method that your application will use to retrieve an RSS feed to use as data to be sorted, and another method that will format the query results as a n HTML document to be displayed using the WebBrowser control.
· In the Form1 class, add the following code.
Private rss As XElement
‘ GetRssDocument: Retrieves an RSS XML document from a supplied URL.
Private Function GetRssDocument(ByVal url As String) As XElement
Dim request = Net.WebRequest.Create(url)
Dim response = request.GetResponse()
Dim reader = Xml.XmlReader.Create(response.GetResponseStream())
Dim rssDoc = XDocument.ReadFrom(reader)
‘ GetHtmlDocument: Returns HTML transformed from a list of XML elements from
‘ an RSS feed.
Private Function GetHtmlDocument(ByVal items As IEnumerable(Of XElement))
Dim htmlDoc = <html>
<table border=“0“ cellspacing=“2“>
From item In items _
<td style=“width:480“><b><%= item.<title>.Value %></b></td>
<td><%= item.<pubDate>.Value %></td>