MSDN Magazine - August 2008 - (Page 31) IncrementalSearch(Of T) will be strongly typed on the search result and will derive from IncrementalSearch. It will override the Search method and provide the underlying strongly typed collection classes. See Figure 7 for the full definition of the refactored IncrementalSearch class. The IncrementalSearch(Of T) class will now have an additional property named Comparer of type IComparer(Of T). When this is present, the results will be sorted after each call to Search: Public NotInheritable Class IncrementalSearch(Of T) Inherits IncrementalSearch Private m_enumerator As IEnumerator(Of T) Private m_allFound As New List(Of T) Private m_comparer As IComparer(Of T) Figure 8 Custom Sorting Private Function GetQueryMethodsOfNameSorted( _ ByVal doc As XDocument, ByVal name As String) As IncrementalSearch Dim e = From method In doc _ Where method.@Name.Contains(name) _ Where 0 = String.Compare(method.@Kind, "Method") _ Join type In doc On type.@Id Equals method.@TypeId _ And type.@AssemblyId Equals method.@AssemblyId _ Select Type = type.@Name, Method = method.@Name Dim x = New IncrementalObjectSearch(e) x.Comparer = New MethodsOfNameComparer() Return x End Function Private Class MethodsOfNameComparer Implements IComparer Public Function Compare(ByVal x As Object, _ ByVal y As Object) As Integer Implements _ System.Collections.IComparer.Compare Dim comp = StrComp(x.Type, y.Type) If comp 0 Then Return comp End If Return StrComp(x.Method, y.Method) End Function End Class Now it’s possible to reuse existing mechanisms to sort a query without introducing eager evaluation: Private Function GetFullMethodNameSorted( _ ByVal doc As XDocument, ByVal name As String) _ As IncrementalSearch Dim e = From method In doc _ Where method.@Name.Contains(name) _ Where 0 = String.Compare(method.@Kind, "Method") _ Join type In doc _ On type.@Id Equals method.@TypeId _ And type.@AssemblyId Equals method.@AssemblyId _ Select type.@Namespace & "." & type.@Name & "." & method.@Name Dim search = IncrementalSearch.Create(e) search.Comparer = StringComparer.Ordinal Return search End Function For handwritten queries, this solution often is enough. However, this is primarily designed to hold the result of LINQ queries that often contain anonymous types. In code you cannot refer to the type of an anonymous type. Instead you must depend on type inference to create strongly typed generic classes. This can be accomplished by adding a factory method named Create onto the IncrementalSearch base class. Since there is only one generic parameter and the argument satisfies this parameter, the compiler can infer the argument and there is no need to explicitly write it out: Dim e = From method In doc _ Where String.Compare(method.@Kind, "Method") = 0 _ Select MethodName = method.@Name ' What is the type name of the element? Dim s1 As New IncrementalSearch(Of ???)(e) ' No need to write the type name Dim s2 = IncrementalSearch.Create(e) To continue supporting weakly typed collections, you can implement another child of IncrementalSearch named IncrementalObjectSearch. This will be similar to the current implementation except that it will be based on an ArrayList instead of a List(Of Object): Public NotInheritable Class IncrementalObjectSearch Inherits IncrementalSearch Private m_enumerator As IEnumerator Private m_allFound As New ArrayList Private m_comparer As IComparer bers are present on the underlying values returned by the query. Using late binding you can access these directly. Figure 8 contains an example of how to create a sorted version of the original query. Since you are using a Comparer instead of an order by statement, this will be a lazy evaluation and will perform well in the UI. This was a fun pattern to implement and write about, but I only have so much space in a column. There are still many other ways to make the UI even more efficient. Here are some ideas that you might want to experiment with on your own: • Reorganizing the source data to be more efficient for queries. • Using LINQ to create more efficiently organized data sets in memory, letting further queries operate against this betterstructured data. • Offloading the query onto another thread. • Providing UI to indicate to the user a search is in progress. • Putting the DataGridView into VirtualMode. • Searching only against previous results rather than the entire document as the user types more text. • Introducing Parallel LINQ queries. Hopefully I’ll have time to expand on these in my blog. Also, be sure to check out the Visual Basic Team Blog (blogs.msdn.com/vbteam) and the MSDN® Visual Basic Developer Center (msdn.com/vbasic) for more information. Sorting Anonymous Types Queries often return a collection of anonymous types. Because you cannot describe the type of an anonymous type in code, it is impossible to write an implementation of IComparer(Of T). Luckily you’re working in Visual Basic here. You know what memmsdnmagazine.com Jared Parsons is a Software Engineer at Microsoft working on the Visual Basic Debugger and IDE. His passion lies in code, coding, and pretty much anything related to programming. He blogs regularly at blogs.msdn.com/jaredpar. August 2008 31 http://blogs.msdn.com/vbteam http://msdn.com/vbasic http://blogs.msdn.com/jaredpar http://msdnmagazine.com
Table of Contents Feed for the Digital Edition of MSDN Magazine - August 2008 MSDN Magazine - August 2008 Toolbox CLR Inside Out Basic Instincts Cutting Edge Patterns in Practice Data 2.0 - Expose And Consume Data In A Web Services World Biztalk EDI - Build A Robust EDI Solution With BizTalk Server Silverlight - Create Animations With XAML And Expression Blend Write On! - Create Web Apps You Can Draw On With Silverlight 2 Wicked Code - Craft Custom Controls For Silverlight 2 Team System Foundations Windows With C++ Concurrent Affairs Going Places { End Bracket } MSDN Magazine - August 2008 MSDN Magazine - August 2008 - (Page Intro) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page Cover1) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page Cover2) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page 1) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page 2) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page 3) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page 4) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page 5) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page 6) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page 7) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page 8) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page 9) MSDN Magazine - August 2008 - MSDN Magazine - August 2008 (Page 10) MSDN Magazine - August 2008 - Toolbox (Page 11) MSDN Magazine - August 2008 - Toolbox (Page 12) MSDN Magazine - August 2008 - Toolbox (Page 13) MSDN Magazine - August 2008 - Toolbox (Page 14) MSDN Magazine - August 2008 - Toolbox (Page 15) MSDN Magazine - August 2008 - Toolbox (Page 16) MSDN Magazine - August 2008 - CLR Inside Out (Page 17) MSDN Magazine - August 2008 - CLR Inside Out (Page 18) MSDN Magazine - August 2008 - CLR Inside Out (Page 19) MSDN Magazine - August 2008 - CLR Inside Out (Page 20) MSDN Magazine - August 2008 - CLR Inside Out (Page 21) MSDN Magazine - August 2008 - CLR Inside Out (Page 22) MSDN Magazine - August 2008 - Basic Instincts (Page 23) MSDN Magazine - August 2008 - Basic Instincts (Page 24) MSDN Magazine - August 2008 - Basic Instincts (Page 25) MSDN Magazine - August 2008 - Basic Instincts (Page 26) MSDN Magazine - August 2008 - Basic Instincts (Page 27) MSDN Magazine - August 2008 - Basic Instincts (Page 28) MSDN Magazine - August 2008 - Basic Instincts (Page 29) MSDN Magazine - August 2008 - Basic Instincts (Page 30) MSDN Magazine - August 2008 - Basic Instincts (Page 31) MSDN Magazine - August 2008 - Basic Instincts (Page 32) MSDN Magazine - August 2008 - Cutting Edge (Page 33) MSDN Magazine - August 2008 - Cutting Edge (Page 34) MSDN Magazine - August 2008 - Cutting Edge (Page 35) MSDN Magazine - August 2008 - Cutting Edge (Page 36) MSDN Magazine - August 2008 - Cutting Edge (Page 37) MSDN Magazine - August 2008 - Cutting Edge (Page 38) MSDN Magazine - August 2008 - Patterns in Practice (Page 39) MSDN Magazine - August 2008 - Patterns in Practice (Page 40) MSDN Magazine - August 2008 - Patterns in Practice (Page 41) MSDN Magazine - August 2008 - Patterns in Practice (Page 42) MSDN Magazine - August 2008 - Patterns in Practice (Page 43) MSDN Magazine - August 2008 - Patterns in Practice (Page 44) MSDN Magazine - August 2008 - Patterns in Practice (Page 45) MSDN Magazine - August 2008 - Patterns in Practice (Page 46) MSDN Magazine - August 2008 - Patterns in Practice (Page 47) MSDN Magazine - August 2008 - Data 2.0 - Expose And Consume Data In A Web Services World (Page 48) MSDN Magazine - August 2008 - Data 2.0 - Expose And Consume Data In A Web Services World (Page 49) MSDN Magazine - August 2008 - Data 2.0 - Expose And Consume Data In A Web Services World (Page 50) MSDN Magazine - August 2008 - Data 2.0 - Expose And Consume Data In A Web Services World (Page 51) MSDN Magazine - August 2008 - Data 2.0 - Expose And Consume Data In A Web Services World (Page 52) MSDN Magazine - August 2008 - Data 2.0 - Expose And Consume Data In A Web Services World (Page 53) MSDN Magazine - August 2008 - Data 2.0 - Expose And Consume Data In A Web Services World (Page 54) MSDN Magazine - August 2008 - Data 2.0 - Expose And Consume Data In A Web Services World (Page 55) MSDN Magazine - August 2008 - Data 2.0 - Expose And Consume Data In A Web Services World (Page 56) MSDN Magazine - August 2008 - Data 2.0 - Expose And Consume Data In A Web Services World (Page 57) MSDN Magazine - August 2008 - Biztalk EDI - Build A Robust EDI Solution With BizTalk Server (Page 58) MSDN Magazine - August 2008 - Biztalk EDI - Build A Robust EDI Solution With BizTalk Server (Page 59) MSDN Magazine - August 2008 - Biztalk EDI - Build A Robust EDI Solution With BizTalk Server (Page 60) MSDN Magazine - August 2008 - Biztalk EDI - Build A Robust EDI Solution With BizTalk Server (Page 61) MSDN Magazine - August 2008 - Biztalk EDI - Build A Robust EDI Solution With BizTalk Server (Page 62) MSDN Magazine - August 2008 - Biztalk EDI - Build A Robust EDI Solution With BizTalk Server (Page 63) MSDN Magazine - August 2008 - Biztalk EDI - Build A Robust EDI Solution With BizTalk Server (Page 64) MSDN Magazine - August 2008 - Biztalk EDI - Build A Robust EDI Solution With BizTalk Server (Page 65) MSDN Magazine - August 2008 - Biztalk EDI - Build A Robust EDI Solution With BizTalk Server (Page 66) MSDN Magazine - August 2008 - Biztalk EDI - Build A Robust EDI Solution With BizTalk Server (Page 67) MSDN Magazine - August 2008 - Silverlight - Create Animations With XAML And Expression Blend (Page 68) MSDN Magazine - August 2008 - Silverlight - Create Animations With XAML And Expression Blend (Page 69) MSDN Magazine - August 2008 - Silverlight - Create Animations With XAML And Expression Blend (Page 70) MSDN Magazine - August 2008 - Silverlight - Create Animations With XAML And Expression Blend (Page 71) MSDN Magazine - August 2008 - Silverlight - Create Animations With XAML And Expression Blend (Page 72) MSDN Magazine - August 2008 - Silverlight - Create Animations With XAML And Expression Blend (Page 73) MSDN Magazine - August 2008 - Silverlight - Create Animations With XAML And Expression Blend (Page 74) MSDN Magazine - August 2008 - Silverlight - Create Animations With XAML And Expression Blend (Page 75) MSDN Magazine - August 2008 - Silverlight - Create Animations With XAML And Expression Blend (Page 76) MSDN Magazine - August 2008 - Silverlight - Create Animations With XAML And Expression Blend (Page 77) MSDN Magazine - August 2008 - Silverlight - Create Animations With XAML And Expression Blend (Page 78) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 79) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 80) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 81) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 82) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 83) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 84) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 85) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 86) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 87) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 88) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 89) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 90) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 91) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 92) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 93) MSDN Magazine - August 2008 - Write On! - Create Web Apps You Can Draw On With Silverlight 2 (Page 94) MSDN Magazine - August 2008 - Wicked Code - Craft Custom Controls For Silverlight 2 (Page 95) MSDN Magazine - August 2008 - Wicked Code - Craft Custom Controls For Silverlight 2 (Page 96) MSDN Magazine - August 2008 - Wicked Code - Craft Custom Controls For Silverlight 2 (Page 97) MSDN Magazine - August 2008 - Wicked Code - Craft Custom Controls For Silverlight 2 (Page 98) MSDN Magazine - August 2008 - Wicked Code - Craft Custom Controls For Silverlight 2 (Page 99) MSDN Magazine - August 2008 - Wicked Code - Craft Custom Controls For Silverlight 2 (Page 100) MSDN Magazine - August 2008 - Wicked Code - Craft Custom Controls For Silverlight 2 (Page 101) MSDN Magazine - August 2008 - Wicked Code - Craft Custom Controls For Silverlight 2 (Page 102) MSDN Magazine - August 2008 - Team System (Page 103) MSDN Magazine - August 2008 - Team System (Page 104) MSDN Magazine - August 2008 - Team System (Page 105) MSDN Magazine - August 2008 - Team System (Page 106) MSDN Magazine - August 2008 - Team System (Page 107) MSDN Magazine - August 2008 - Team System (Page 108) MSDN Magazine - August 2008 - Foundations (Page 109) MSDN Magazine - August 2008 - Foundations (Page 110) MSDN Magazine - August 2008 - Foundations (Page 111) MSDN Magazine - August 2008 - Foundations (Page 112) MSDN Magazine - August 2008 - Foundations (Page 113) MSDN Magazine - August 2008 - Foundations (Page 114) MSDN Magazine - August 2008 - Windows With C++ (Page 115) MSDN Magazine - August 2008 - Windows With C++ (Page 116) MSDN Magazine - August 2008 - Windows With C++ (Page 117) MSDN Magazine - August 2008 - Windows With C++ (Page 118) MSDN Magazine - August 2008 - Windows With C++ (Page 119) MSDN Magazine - August 2008 - Windows With C++ (Page 120) MSDN Magazine - August 2008 - Windows With C++ (Page 121) MSDN Magazine - August 2008 - Windows With C++ (Page 122) MSDN Magazine - August 2008 - Concurrent Affairs (Page 123) MSDN Magazine - August 2008 - Concurrent Affairs (Page 124) MSDN Magazine - August 2008 - Concurrent Affairs (Page 125) MSDN Magazine - August 2008 - Concurrent Affairs (Page 126) MSDN Magazine - August 2008 - Concurrent Affairs (Page 127) MSDN Magazine - August 2008 - Concurrent Affairs (Page 128) MSDN Magazine - August 2008 - Concurrent Affairs (Page 129) MSDN Magazine - August 2008 - Concurrent Affairs (Page 130) MSDN Magazine - August 2008 - Going Places (Page 131) MSDN Magazine - August 2008 - Going Places (Page 132) MSDN Magazine - August 2008 - Going Places (Page 133) MSDN Magazine - August 2008 - Going Places (Page 134) MSDN Magazine - August 2008 - Going Places (Page 135) MSDN Magazine - August 2008 - { End Bracket } (Page 136) MSDN Magazine - August 2008 - { End Bracket } (Page Cover3) MSDN Magazine - August 2008 - { End Bracket } (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.