In the previous essay, I showed you the exciting new features of Linq in Visual Basic 9 through simple code. This essay, in a more complex example, demonstrates the flexibility and extensibility of LINQ to explore how LINQ works and advanced applications.
Lack of LINQ
The. Net Framework 3.5 Beta 1, the application of LINQ also has certain limitations, such as not supporting Group by,having, and Join syntax. Also, it has more or less bugs, so it is not recommended for environments that require high stability. Of course, it is believed that in the final release, it will greatly improve its capabilities, after all, this is the most important new feature in the. Net Framework 3.5.
About anonymous Types
In fact, in Visual Basic 9 & C # 3.0, support for anonymous types has been completed. For example:
Visual Basic 9 Examples of anonymous types
Dim Person = New With {.Name = "John Chen", .Age = 13}
Console.WriteLine("Name:" & Person.Name)
Console.WriteLine("Age:" & Person.Age)
This is a very useful syntax feature for LINQ, although it is also a compile-time behavior. When it is applied in Linq, you can use the Select statement to implicitly create a set of anonymous type objects.
Note: In Linq, the Select statement is reset, which is needed for IntelliSense in the IDE.
What does an anonymous type do?
Anonymous type generation code for person
Public Class _ClosureClass _Closure$__10
' Methods
<DebuggerNonUserCode> _
Public Sub New()Sub New()
<DebuggerNonUserCode> _
Public Sub New()Sub New(ByVal other As _Closure$__10)
' Fields
Public $VB$Local_Name As String
Public $VB$Local_Age As Integer
End Class
In fact, it generates a Closure type at compile time, encapsulating this seemingly dynamic anonymous type. Dynamic Interface, however, has been canceled.
What has Linq done?
First, it must be understood that LINQ is actually a compile-time behavior. The compiler converts LINQ expressions into obscure nested calls to maintain the. Net 2.0 compatibility. IQueryable came into being, providing a large number of query functions to facilitate nesting calls, and extended methods to enable the. Net 2.0 original collection types to support Linq syntax characteristics. Therefore, do not worry about the compatibility issues that LINQ brings.
Sorting in Linq
Of course, you can use the by function in IEnumerable (of T), but I do not recommend that. You can use exactly the same syntax as T-sql:
Dim Data As String() = {"a", "b", "d", "c"}
Dim Query = From O In Data Order By O Descending '这就是核心
'输出信息
For Each QItem In Query
Console.WriteLine(QItem)
Next
Cross-array queries in LINQ
In the current Visual Basic 9 Beta 1, the use of Join, Union, and other advanced class SQL features in query statements is not supported, so it is only an extremely complex approach to directly invoke the Union,order by function in IEnumerable (of T) 。 However, you can still use some workaround to achieve the desired functionality. For example:
Examples of cross-array queries in LINQ
Dim CLRLanguage() As String = {"Visual Basic", "C#", "J#", "C++/CLI"}
Dim CLRCount() As KeyValuePair(Of String, Integer) = {New KeyValuePair(Of String, Integer)("Visual Basic", 1000000), _
New KeyValuePair(Of String, Integer)("C#", 1200000), _
New KeyValuePair(Of String, Integer)("J#", 300000), _
New KeyValuePair(Of String, Integer)("C++/CLI", 600000)}
Dim Query = (From O As String In CLRLanguage Order By (From Count In CLRCount Where Count.Key = O Select Count.Value))
Restudying
Visual Basic 9 Incomplete Introductory series (1): Grammar candy
Visual Basic 9 Incomplete Getting Started series (2): Syntax enhancement
Visual Basic 9 Incomplete Getting Started series (3): Introduction to LINQ
Conclusion
Linq is essentially still a compile-time behavior, which determines that it is impossible to make too many changes, but the rapid development momentum it represents is hard to reverse. After all, no one wants to use arcane grammar to write obscure programs, even if it has a certain improvement in performance. The recent revision busy, combined with heavy schoolwork, writing speed had to slow down, so forgive me. At the same time, I hope you can focus on the Visual Basic team and we will try to provide better technical essays.