Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

New in Visual Basic 2008 - 9.0

Vorig Onderwerp

Object Oriented Programming

LINQ - Language Integrated Query

Vorig Onderwerp

Query Methods

Query Expression

Query Expression

Option Infer On

Option Infer On

LINQ to Objects

LINQ to Objects



LINQ ( Language INtegrated Query ) makes it possible to query data with a SQL-like synthax.

New features like query methods ( added through extension methods ), lambda functions and anonymous types make it possible for language like Visual Basic ( but also C# ) to introduce LINQ.

LINQ offers a more declarative manner of stating what needs to happen with certain data, instead of more complex algorithms ( like selections and iterations ) that specify how something needs to happen.


Query Expression


Using query methods next example could select all persons from "New York", sorted on their names.


Option Infer On
Option Strict On
Public Class Person
    Private m_Name As String
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property
    Private m_City As String
    Public Property City() As String
        Get
            Return m_City
        End Get
        Set(ByVal value As String)
            m_City = value
        End Set
    End Property
    Private m_IsMale As Boolean
    Public Property IsMale() As Boolean
        Get
            Return m_IsMale
        End Get
        Set(ByVal value As Boolean)
            m_IsMale = value
        End Set
    End Property
    Public Overrides Function ToString() As String
        ToString = "Female "
        If IsMale Then ToString = "Male "
        ToString &= Name & ", from " & City & "."
    End Function
End Class
Public Class Example1
    Public Shared Sub Main()
        Dim persons As Person() = New Person() { _
          New Person With {.Name = "John", .City = "New York", .IsMale = True}, _
          New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _
          New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _
          New Person With {.Name = "Paul", .City = "New York", .IsMale = True}}
        '
        Dim personsNY As IEnumerable(Of Person) = _
                     persons.Where(Function(person) person.City = "New York") _
                            .OrderBy(Function(person) person.Name) _
                            .Select(Function(person) person)
        '
        For Each person As Person In personsNY
            Console.WriteLine(person)
        Next
        '
        Console.ReadLine()
    End Sub
End Class
Download Broncode

Output :

 Female Bo, from New York.
 Male John, from New York.
 Male Paul, from New York.

The same can be done by a query expression ( LINQ expression ).


Public Class Example2
    Public Shared Sub Main()
        Dim persons As Person() = New Person() { _
          New Person With {.Name = "John", .City = "New York", .IsMale = True}, _
          New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _
          New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _
          New Person With {.Name = "Paul", .City = "New York", .IsMale = True}}
        '
        Dim personsNY As IEnumerable(Of Person) = _
                                                From person In persons _
                                                Where person.City = "New York" _
                                                Order By person.Name _
                                                Select person
        '
        For Each person As Person In personsNY
            Console.WriteLine(person)
        Next
        '
        Console.ReadLine()
    End Sub
End Class
Download Broncode

Output :

 Female Bo, from New York.
 Male John, from New York.
 Male Paul, from New York.

The expression is shorter, and probably easier to compose than the expression using query methods.

The query expression is nothing more than a shorthand notation for the equivalent expression of Example1.

The query expression can use SQL-like operators as Where, Select, Group By, Order By, Then By, ..., at least when the System.Linq namespace is imported, only then the underlaying query methods are in scope.

You've probably noticed how the 'From' clause precedes the other clauses, this in contrast to most SQL dialects, where 'From' usually is the last clause. In LINQ this is necessary because the 'From' clause specifies the informationsource, and introduces a variable ( starts the scope of that variable ) that can be used in the other clauses of the query expressions to refer to aspects ( for instance elements ) of that source.


Klik hier om terug naar boven te gaan.  Up


Option Infer On


Above examples could be compiled under Option Infer Off, this however is not always the case.

When next example uses a Select case, to declare that we are only interested in the names and cities of the selected persons, Option Infer On is necessary.

The selector projects the result in objects of an anonymous type ( with two properties, Name and City ). The result of this query will be of type IEnumerable(Of some-anonymous-type). The declarations of variables personNamesAndCitiesNY and personNameAndCity could therefore not specify a type.


Public Class Example3
    Public Shared Sub Main()
        Dim persons = New Person() { _
          New Person With {.Name = "John", .City = "New York", .IsMale = True}, _
          New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _
          New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _
          New Person With {.Name = "Paul", .City = "New York", .IsMale = True}}
        '
        Dim personNamesAndCitiesNY = From person In persons _
                                     Where person.City = "New York" _
                                     Order By person.Name _
                                     Select person.Name, person.City
        '
        For Each personNameAndCity In personNamesAndCitiesNY
            Console.WriteLine(personNameAndCity.Name & " from " & _
                              personNameAndCity.City)
        Next
        '
        Console.ReadLine()
    End Sub
End Class
Download Broncode

Output :

 Bo from New York
 John from New York
 Paul from New York

Klik hier om terug naar boven te gaan.  Up


LINQ to Objects


The above examples use query expressions ( LINQ ) to query elements of objectcollections. As long as these collections are IEnumerable or IEnumerable(Of T), LINQ can be used to query them. This is called "LINQ to objects".

LINQ can also be used to query other types of data, for instance relational data by "LINQ to ADO.NET" ( "LINQ to SQL" and "LINQ to DataSet" ) or by "LINQ to Entity" ). Also XML can be queried, using "LINQ to XML".
Query expressions can be used as long as a LINQ provider or API is defined, that specifies how queries on those sources can be evaluated.
This offers us an uniform way of working on or querying different types of data.


Updated On : 2008-10-01

Download Broncode

Published On : 2008-11-06

LINQ - Language Integrated Query

Vorig Onderwerp

Query Methods

New in Visual Basic 2008 - 9.0

Vorig Onderwerp

Object Oriented Programming

  Home  
Nederlands
Nederlands

Add to favorites (IE).


No printable version available.