Wooley's LINQ Wonderings

Insights and observations regarding LINQ

VB 9.0 documentation

The VB 9.0 documentation was recently updated. Unfortunately, it doesn't agree with the current bits. If you haven't read the documents from the previous LINQ CTP's, check it out at http://msdn2.microsoft.com/en-us/library/ms364068(vs.80).aspx.

I took a couple minutes looking it over and it does give a quick glimpse of the basic underlying concepts that hopefully are coming. Unfortunately, there are a number of items in the documentation that don't appear to be included in the current CTP. I am definitely hoping that they do make it in the next drop. The features that are discussed but not yet included are: Joins, Lambda's, and Nullable types. In addition, the samples seem to use an Auto-Implemented Property syntax as introduced in the current C#, but reading more closely, they are just using a pseudo code syntax. Let's take a look at each item.

Auto-Implemented Property
The March CTP of C# 3.0 includes an abbreviated syntax for creating a property with a private backing field. With this, you can declare a property in a class with the following:

string Foo{ get; set; }

See my full write-up for more details on the C# implementation. The VB document uses a similar syntax to declare their sample Country class.

Class Country
Public Property Name As String
Public Property Area As Long
Public Property Population As Integer
End Class

Unfortunately, this syntax does not work. (Reading the sentence in the documentation prior to the sample, we find that it is just "pseudo code used for brevity"). Honestly, I am somewhat skeptical of the practical usefulness of the Autoimplemented property syntax, so I don't mind the omission. I would prefer if they would just use public fields in the documentation rather than properties so that people can copy and paste the code without syntax errors.

I'm confident that the final drop of VB 9.0 will include a syntax for Joins. Unfortunately, the syntax in the latest documentation does not agree with the March CTP. Their sample query is as follows:

Dim countriesByClimate = _
  From country In Countries _
 Join city In Capitals On country.Name Equals city.Country _
 Group By country.IsTropical()
 Into Total = Count(), Density = Average(country.Population/country.Area)

The current CTP requires this query to be written using ANSI-82 syntax with the join in the where clause as follows:

Dim countriesByClimate = _
   From country in Countries, _
            city in Capitals _
   Where country.Name=city.Country _
   Group By country.IsTropical() _
   Into Total = Count(), _
       Density = Average(country.Populate/country.Area)

Also, note that their code occasionally is missing necessary continuation characters. I definitely look forward to Joins making it into VB on a future build.

Nullable Types
The Dotnet 2.0 framework included support for Nullable types. Whereas C# was allowed a shortened syntax using a question mark (DateTime? foo), VB coders are forced to use the native Nullable(Of DateTime) syntax. The current documentation doesn't appear to be changed from the May CTP indicating that VB will be able to leverage the DateTime? syntax. As of the March CTP, this is still not supported at this point. I don't know if this is an oversight or foreshadowing of things to come.

Lambda Expression support
The documentation also indicates a proposed syntax for Lambda expressions for VB using a "Function" signature as follows:

Function (Country As Country) country.Population < 1000000

I have long thought that Lambda support (or some equivalent for anonymous delegates) was a big omission that VB really needed, particularly as more extension methods are appearing which require the delegate as a parameter, particularly the new DataShape methods to give query hints as to which values to pre-fetch when querying with LINQ to SQL. The new syntax, could also be useful for existing 2.0 properties which take a predicate as a parameter (like Find and Sort). With the new syntax, we can finally rewrite our expressions and leverage some of the other extension methods that do not have query expression syntax at the moment. Consider the following Query expression:

dim x = From c In Customers
            Where c.Country = "London"
            Select c.Name

C# can translate this into the following with Extension Methods:

var x = Customers.Where(c => c.Country=="London")
      .Select(c => c.Name);

With Lambda's in VB, we can finally do the same:

dim x = Customers.Where(Function(c as Customer) c.Country="London") _
       .Select(Function(c as Customer) c.Name)

Although this example isn't ground breaking. The availability of Lambdas in VB opens up a number of possibilities. I would love to test it, but they are not yet supported in the March CTP. Once these features make it into the bits, I will let you know all about them. Thanks to Bill McCarthy for pointing out the updated docs.

Crossposted from http://devauthority.com/blogs/jwooley/default.aspx
Published Monday, March 26, 2007 1:47 AM by jwooley
Filed under:
Anonymous comments are disabled