Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

What's New in Visual Basic 9.0? Part 2 : Page 2

The release of Visual Studio 2008 updates Visual Basic to version 9.0, with several key language enhancements and LINQ support. Part Two of this series will walk you through VB 9's new LINQ support features and other time-saving enhancements.


advertisement
LINQ to Dataset
Besides manipulating data in memory, you can also use LINQ to query data stored in structures like datasets and datatables. The following example shows how the Authors table within the pubs sample database is loaded onto a Dataset object and then queried using LINQ to Dataset:

Dim conn As SqlConnection Dim comm As SqlCommand Dim adapter As SqlDataAdapter Dim ds As New DataSet '---load the Authors table into the dataset--- conn = New SqlConnection("Data Source=.\SQLEXPRESS;" & _ "Initial Catalog=pubs;Integrated Security=True") comm = New SqlCommand("SELECT * FROM Authors", conn) adapter = New SqlDataAdapter(comm) adapter.Fill(ds) '---query for authors living in CA--- Dim authors = From author In ds.Tables(0).AsEnumerable _ Where author.Field(Of String)("State") = "CA" _ Select author

To display the result, you can bind the result to a DataGridView control using the AsDataView() method:

'---bind to a datagridview control--- DataGridView1.DataSource = authors.AsDataView()

Or, you can iteratively loop through the result using a For Each loop:


For Each row As DataRow In authors Console.WriteLine("{0} - {1}, {2}", _ row("au_id"), row("au_fname"), row("au_lname")) Next

If you want to query the authors based on their contract status, use the following query:

Dim authors = From author In ds.Tables(0).AsEnumerable _ Where author.Field(Of Boolean)("Contract") = True _ Select author

The earlier section mentioned the anonymous types feature, new in VB 9.0. Using this feature, you can define a new type without needing to define a new class. To demonstrate a good use of anonymous types, consider the following statement:

'---query for authors living in CA--- Dim authors = From author In ds.Tables(0).AsEnumerable _ Where author.Field(Of String)("State") = "CA" _ Select New With { _ .ID = author.Field(Of String)("au_id"), _ .FirstName = author.Field(Of String)("au_fname"), _ .LastName = author.Field(Of String)("au_lname") _ }

Figure 1. Intellisense Knows: Enter the word "authors" and IntelliSense will return its type.

Here, you select all the authors living in the state of CA while simultaneously creating a new type consisting of three properties: ID, FirstName, and LastName. Now, type in the word "authors" and IntelliSense will show you that authors is of type EnumerableRowCollection (Of ) (see Figure 1).

You can now print out the result using a For Each loop:

For Each auth In authors Console.WriteLine("{0} - {1}, {2}", _ auth.ID, auth.FirstName, auth.LastName) Next



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap