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.