Friday, July 13, 2007

New LINQ with Visual Basic Articles

The VB Team has lagged the C# group in implementing LINQ-related keywords and language features throughout the VS 2008 beta process. This situation is understandable because the C# group is in charge of the core LINQ specification, so the VB compiler group can't complete their work until the C# implementation is fully baked.

The same has been true for LINQ documentation, but it's less understandable. For example, the May 2006 .NET Standard Query Operators white paper has only C# 3.0 sample code. Until Wednesday, I hadn't seen a single working example of an expression tree or custom LINQ implementation written in VB; they were all C#-based.

LINQ to WDS

The tide may be turning with Kevin Halverson's LINQ to WDS project. (WDS = Windows Desktop Search.) In How to implement IQueryable (Part 1) and How to implement IQueryable (Part 2), Kevin delves into a detailed explanation of how to create a custom LINQ implementation that lets you execute queries against the file system with WDS's OLE DB driver. WDS is part of Vista, but you can download versions for Windows XP and Windows Server 2003 here. The project translates simple LINQ Select and Where clauses into SQL statements like this:

SELECT System.ItemPathDisplay FROM SystemIndex WHERE NOT CONTAINS(System.ItemType, 'folder') AND (System.DateCreated >= 'EarliestDate' AND System.FileName LIKE 'Expression')

Kevin's Part 1 post provides a link to download the complete source code, which totals about 500 lines. The UI is a bit Spartan: A message box wrapped by a For Each loop substitutes for the usual console output.

Note: Kevin says the project requires Beta 2 because IQueryable was been refactored after Beta 1 released. However, it works for me on the Orcas June 2007 CTP.

VB Query [In]Comprehensions

Scott Wisniewski's "Basic Instincts: Query Comprehensions" column in the August 2007 issue of MSDN Magazine covers VB's query translation, lazy evaluation, the Select, SelectMany, Where and Join query operators, and expression trees in much greater depth than I've seen before (other than in Kevin's two posts).

Scott includes a sample project that instruments most of the 48 (or 50 if you count TakeWhile and SkipWhile separately) Standard Query Operators with PerfMon counters. The current version throws an error when you open the solution because of a missing unit Test.vbproj project file. You can fix that error by removing its entry from Microsoft.Linq.Instrumentation.sln.

There's a 45-KB EULA but not even a Readme file to explain how to use the WrappedEnumerable, WrappedEnumerator, and WrappedOrderedCollection classes. I couldn't find a single comment in the project. This makes the sample project incomprehensible to most LINQ neophytes.

The VB Team must have been confident that Beta 2 would be available by the magazine's posting date because Scott's project does require Beta 2. (It needs Beta 2's IOrderedSequence interface and CreateOrderedSequence method to compile; they aren't in the Orcas June 2007 CTP.)

Maybe Scott will add some documetation to his Shameless Plug (Scott Wisniewski) post.

0 comments: