A few of the many syntax changes in VB (and Linq for SQL queries)

VB9 has come a long way in the new March CTP of Orcas! Paul Vick has a quick list of what is now in there. And there are a lot of good details in the msdn documentaiton that comes along with the CTP.

A few things I noticed quickly when bringing my LINQ for SQL demos over to the new bits.

  • First, and happily, the named parameter syntax of := is no longer necessary. You can just type =, like a normal person.
  • You need to be explicit about using the reference to the entity when refering to properties. In other words where I could say
     From s In db.Suppliers_
                Where CompanyName = “Exotic Liquids” …
    I now need to put  “s.” in front of the CompanyName property
  • The anonymous types have been fleshed out more and you need to use the With keyword when creating one.
  • Also in the anonymous types, when you are definining a new named parameter, you need the “.” in front of that parameter, so it is obviously a property of the anonymous type.
  • Maybe this isn’t the ultimate way to do it, but it seems that if you want to further leverage one of the properties of that anonymous type, eg to order on, then you need to name the anonymous type and reference it in the order by clause. Again, maybe this isn’t THE way, but it’s the way I got my code working again.

Here’s a before (as in earlier CTP) and an after (March CTP) example of a simple query that does all of these things.

OLD
From s In db.Suppliers _
            Select New {Company := CompanyName, Country, Products}

NEW
From s In db.Suppliers _
            Select New With {.Company = s.CompanyName, s.Country, s.Products}

Here’s what it looks like with a nested query
  From s In db.Suppliers _
            Select New With {.Company = s.CompanyName, s.Country, _
            .Products = (From p In s.Products _
                Select New With {p.ProductName})}

OLD with Order By
From s In db.Suppliers _
            Select New {s.CompanyName, s.Country, s.Products} _
            Order By CompanyName

NEW with Order By
  Dim mylist = From s In db.Suppliers _
            Select s2 = New With {s.CompanyName, s.Country, s.Products} _
            Order By s2.CompanyName

I’ve seen samples where “s” has been used for both the reference to the items in the db.suppliers collection AND to the name of the new anonymous type. That scares me, so I used a new variable (s2).

There is also a whole new set of query samples in the MSDN documentation.

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

5 thoughts on “A few of the many syntax changes in VB (and Linq for SQL queries)

  1. Can someone tell me what’s the line continuation character in javascript?iam trying to do the following:if (!x && !y && !d ……continue to next line….)

  2. Vijay. That has absolutely nothing to do with this post. C’mon. There are a million places you can ask that question. So I am not going to answer it here. Though it’s REALLY easy. 🙂

  3. sorry. my questions were not appearing on your blog, so i just shoot one randomly but i would never refuse to answer a question!sure there are a million pages but there’s just one julie!

  4. VijayThere is none. IN the example below I just put a return after the parentheses. If I split up text I need to concatenate it.function Button_onclick() {alert("this line"+ " is split up");}But really, it’s not fair for you to do this. There are so many public forums. And if yougoogle "javascript line continuation" you’d have found this just as easily.

Leave a Reply to Roger Jennings Cancel reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.