Visual Basic LINQ Tutorial

LINQ, which stands for Language-Integrated Query, is a feature of the Visual Basic language that lets you perform queries and other operations on data sources. One type of data source is XML code, whose user-defined tags can be used as database fields. This tutorial allows the user to do a query of a simple XML data table. The user enters the title of a movie, which is formatted into a LINQ query. The query seeks the movie with the matching title and returns the director's name from the matching movie.

Setting up the Project

    Before typing the program, you will need to set up a project in Visual Basic. Choose File > New Project > Windows Forms Application. In the Designer window, place a text box and a button on the user form. Double click the button control and add this code to the body of the Button1_Click event:

    XLinqQuery(TextBox1.Text)

    That statement calls a function that does the LINQ database query when the user presses the button on your form. Above the Button1_Click function, type the function:

    Public Sub XLinqQuery(ByVal strMovie As String)

    Dim myMovies = _

    Star Wars

    Excalibur

    Dim query = _
    From m In myMovies. _
    Where m.Value = strMovie _
    Select m

    For Each result In query
    TextBox2.Text = result.@director
    Next
    End Sub

Running the Program

    Run the program by pressing "F5." In the text box, enter "Star Wars" and press the button. The program will query the database to find the director of the movie whose title you entered. After seeing "George Lucas" appear, enter a different movie title: "Excalibur," and press the button to see director John Boorman's name appear.

Exploring the Code

    Refer to the function XlinqQuery, which does the database work. The mini inline database the LINQ query runs on is a piece of XML code. You can use LINQ to query other kinds of databases, both remote (online) and those stored on your computer.

The Actual LINQ

    The actual LINQ query is as follows:

    Dim query = _
    From m In myMovies. _
    Where m.Value = strMovie _
    Select m

    Although its type is not explicitly given, the "query" following the Dim statement is a variable. The complete Dim query statement does not execute the query, it only defines it. The statement that actually executes the query is the following:

    For Each result In query
    TextBox2.Text = result.@director
    Next

    For each iteration of that loop, the result identifier holds the output of the query, which is all data in between the XML tags and , for one particular pair of those tags. The particular attribute we pull out of the result variable is the director attribute, though you could also choose @year_released.

    You can learn about LINQ queries by tinkering with this program. Start by changing the movie database. Add more movies and directors--or change the database completely to reflect your own interests. Add your own fields--aka attributes--to replace the director and year_released fields from the movie database. Instead of searching for a database entry by its name (e.g., "Star Wars"), you can search by field/attribute. If you searched by the year_released attribute in the movie database, the Where clause in your query would look like this: Where m.@year_released = intYear. Here, the ampersand tells the query engine that you want to match on an XML attribute, as opposed to a value (e.g., "Star Wars").

Blog Archive