Wednesday, January 18, 2006

Visual Basic Team Releases January LINQ CTP

Microsoft released the January CTP of the Language Integrated Query (LINQ) project's Visual Basic preview for Visual Studio 2005 on January 17, 2006. The big news is that the January CTP finally adds DLinq support to Visual Basic 9.0. You can download the January bits here. The new CTP replaces the September (PDC 2005) Visual Basic CTP that, unlike the C# version, didn't support DLinq for relational data sources. [Updated 2/7/2006]. The January CTP requires the release version of Visual Studio 2005 or Visual Basic Express, and works with SQL Server 2005 or SQL Server Express. (It probably also works with SQL Server 2000 or MSDE 2000, but I haven't tested that capability.) Microsoft hasn't tested the CTP with languages other than English. Although not stated in the Readme page, it's a good idea to use Control Panel's Add/Remove Programs tool to remove the September CTP version before executing Linq VB Preview (Jan 2006).msi. Running the CTP's installer adds a VB LINQ Preview item to your Programs menu with Readme, Visual Basic 9.0 Overview, Visual Basic 9.0 Samples, Visual Basic LINQ Project Overview, and XLINQ Overview choices.

SQLMetal.exe Updated for Visual Basic 9.0

The January CTP's version of the SQLMetal.exe utility creates the DatabaseName.vb class file that's necessary for mapping an SQL Server resultset to a LINQ object. (The September CTP's SqlMetal.exe didn't work with the /language:vb switch.) Later VB 9.0 versions will add a designer to simplify generating the mapping class file.

To create a Northwind.vb class file with nwind as its namespace by attaching the Northwind.mdf file in the C:\Program Files\VB LINQ Preview\Samples\Sample Queries\SampleQueries folder to your default SQL Server 2005 instance, execute the following from the "\Program Files\VB LINQ Preview\Samples\Sample Queries\SampleQueries folder:

"\program files\vb linq preview\bin\sqlmetal" /namespace:nwind /language:vb /code:Northwind.vb northwind.mdf

If you've already added a Northwind database to your default SQL Server instance, you receive a "Failed to attach an auto-named database for file northwind.mdf. A database with the same name exists ..." or "A connection was successfully established with the server, but then an error occurred during the loginprocess. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)" exception.

In this case, execute the following to create the class file from the server and database you specify in the following example, which assumes SQL Server Windows authentication:

"\program files\vb linq preview\bin\sqlmetal" /server:localhost /database:Northwind /namespace:nwind /language:vb /code:Northwind.vb

New January CTP Features

According to the January CTP's Readme page, the CTP adds the following new features to the September release:

LINQ Intellisense: Query statements now have statement completion, Intellisense, and Pretty Printing to improve overall discovery and readability of VB queries. DLinq Support: Previous tech previews of the LINQ CTP did not fully support leveraging the DLinq technology which is for querying against relational data stores, such as SQL Server. DLinq support in this release is limited in that only the working query syntax over objects will extend to DLinq. Editing support for XML Literals: The Visual Basic background compiler provides immediate feedback, with this release this support has extended to integrated XML. Specifically, XML literals are now automatically indented and XML literals plug into auto-correction. The XML literals are now completely colorized with customizability through the VS 2005 Options, Fonts and colors.
The following new features apply to XML late-binding:
Importing XML Namespaces: XML namespaces can be imported to the VB program using similar mechanism to importing CLR namespace. Accessing Elements with Namespaces: Once an XML namespace is imported, you can use qualified identifiers to access elements with namespaces. Indexer for IEnumerable(Of T): An indexer was added to IEnumerable of any type. This enables easy access to any element from a generic IEnumerable, specifically for accessing the first XElement from the XElements collection that is returned from XML late binding expressions.

Paul Vick Summarizes New January CTP Features

Paul's February 6, 2006 post on the availability of the January CTP was a bit late, but his informal summary of the new DLinq and XLinq features is quite useful:

  • We now support some Intellisense for Select expressions. This is a step forward in our investigation of the Select/From vs. From/Select question, so we’re definitely interested in feedback here.
  • We now support a lot more LINQ, specifically DLinq and variable capture (so you can now access local variables in queries). A huge chunk of my November/December went towards implementing lambda expressions and expression trees in Visual Basic and that, combined with some excellent work by another team member on variable capture, means a lot of stuff works now that didn’t before.
  • The editing/display experience for XML literals has been greatly enhanced — just having colorization makes a huge difference.
  • A bunch of extensions were made to the XLinq support to make working with namespaces and elements easier.
Here's a screen capture that demonstrates colorization of part of the code for generating XLinq XML literals with element values obtained with DLinq from the Northwind sample database: Online Demo and Newsgroup Paul Vick and Amanda Silver deliver "Visual Basic Futures: Latest Developments in the LINQ Project", a 10-minute video overview of the objectives of the LINQ project, plus a brief description of January CTP's new and improved DLinq and XLinq features. Amanda continues the presentation with a 20-minute demonstration of DLinq and XLinq in Visual Studio 2005 console applications that demonstrate basic DLinq and XLinq syntax in simple console applications, the LINQ VB Project Sample Query Explorer Windows form project, and a new (but apparently unfinished) visualizer that displays expression trees for LINQ queries, as shown here:

Unfortunately, the expression tree visualizer isn't included with the January CTP bits. Click here to read a trasncript of the overview and demo. You can post questions about the new VB 9.0 features and (hopefully) receive answers on the LINQ (General) newsgroup. The newsgroup's MVPs are mostly—or, more likely, entirely—C# coders. Sample Applications

The Programs, VB LINQ Preview, Visual Basic 9.0 Samples menu choice opens the [C:]\Program Files\VB LINQ Preview\Samples folder, which contains Hands On Labs, Sample Queries, Visual Basic 9.0 Survey, and XML DOM Survey subfolders. Following is a screen capture of the LINQ VB Project Sample Query Explorer form that opens when you double-click the [C:]\Program Files\VB LINQ Preview\Samples\Sample Queries\SampleQueries.sln solution file and run the 101 DLinq Samples JOIN SelectMany - 1 To Many - 1 sample (DLinq27()).

Note: The default connection string is: connString = "AttachDBFileName='C:\Program Files\VB LINQ Preview\Samples\Sample Queries\SampleQueries\Northwind.mdf'; Server='.\SQLEXPRESS';Integrated Security=SSPI;enlist=false". If you don't have SQL Server Express installed or you already have added the Northwind sample database, you must change the connection string in the DLinqSamples class's Declarations section to a value that suits your installation.

This project lets you execute the sample LINQ, DLinq, and XLinq queries and, for DLinq, display the T-SQL statement and, where applicable, the query resultset for the sample.

The other two sample projects are console applications that are similar to their corresponding September CTP counterparts.

Links to Earlier LINQ Articles and OakLeaf Blog Posts

"Comparing LINQ and Its Contemporaries" by Ted Neward from the .NET Framework Developer Center (December 2005)

"Late Binding in Visual Basic" by Paul Vick and Erik Meijer (December 1, 2005, 00:22:45 MSDN TV video)

"Streamline Mapping With Orcas and LINQ" article from the December 2005 issue of Visual Studio Magazine.

"Cure Data Type 'Impedance Mismatch' With LINQ," from Fawcette Technical Publishing's .NETInsight newsletters for PDC 2005 (September, 2005)

"The Language Integrated Query (LINQ) Project" (first LINQ post), September 14, 2005

"Erik Meijer on LINQ as a 6GL Language Feature," September 14, 2005

"LINQ Interview with Anders Hejlsberg and Paul Vick," September 19, 2005

"Jon Udell: Microsoft driving toward .Net unity," September 21, 2005

"XLinq Presentation at the XML 2005 Conference," September 23, 2005

"Comparing C# 3.0 and VB 9.0 LINQ Syntax," September 26, 2005

"More on Object-XML and Object-Relational Impedance Mismatch," September 29, 2005

"Jon Udell: LINQ 101," September 29, 2005

"Compose XML Content with XLinq Expressions and VB 9.0 XML Literals," October 1, 2005

"More on Visual Basic 9.0 and LINQ versus SQL," October 10, 2005

--rj Technorati: