To understand why LINQ is revolutionary, the best way is beginning with some code examples. In the next chapters you get a huge quantity of code snippets, but this chapter offers basic queries to provide a high-level comprehension. Imagine you have a Person
class exposing the FirstName
, LastName
, and Age
properties. Then, imagine you have a collection of Person
objects, of type List(Of Person)
. Last, imagine you want to extract from the collection all Person
instances whose LastName
property begins with the D
letter. This scenario is accomplished via the following code snippet that uses a LINQ query:
This form of code is known as query expression because it allows extracting from a data source only a subset of data according to specific criteria. Notice how query expressions are performed using some keywords that recall the SQL syntax, such as From
, Where
, Order By
, and Select
. Such keywords are also known as clauses, and the Visual Basic grammar offers a large set of clauses that is examined in detail in the next chapters. The first consideration is that while typing code IntelliSense speeds up your coding experience, providing the usual appropriate suggestions. This is due to the integration of clauses with the language. Second, because clauses are part of the language, you can take advantage of the background compiler that determines if a query expression fails before running the application. Now let’s examine what the query does. The From
clause specifies the data source to be queried, in this case a collection of Person
objects, which allows specifying a condition that is considered if evaluated to True
; in the above example, each Person
instance is taken in consideration only if its LastName
property begins with the D
letter. The Order By
clause allows sorting the result of the query depending on the specified criteria that here is the value of the Age
property in descending order. The Select
clause extracts objects and pulls them into a new IEnumerable(Of Person)
collection that is the type for the peopleQuery
variable. Although this type has not been explicitly assigned, local type inference is used, and the Visual Basic compiler automatically infers the appropriate type as the query result. Another interesting consideration is that queries are now strongly typed. You are not writing queries as strings because you work with reserved keywords and .NET objects, and therefore your code can take advantage of the Common Language Runtime control, allowing better results at both compile time and at runtime. Working in a strongly typed way is one of the greatest LINQ features thanks to its integration with the CLR.
Differently from Visual Basic 2008, in Visual Basic 2010 you can omit the underscore (_) character within LINQ queries as demonstrated by the previous code.
The same result can be obtained by querying a data source with extension methods. The following code demonstrates this:
The .NET Framework offers extension methods that replicate Visual Basic and Visual C# reserved keywords and that receives lambda expressions as arguments pointing to the data source.
18.226.93.137