Lambdas, and Take While, and Group By, Oh My!

Hooray! Visual Basic 2008 Beta2 has been released to the wild today to return to its natural habitat – the desktops of our beloved customers. (Please note our smiling GPM’s face on that landing page — he’s so happy the Beta is done.)


What’s inside? So much that there might be more new features than we released in Beta1, but who’s counting? Here’s a laundry list that’s specific to the VB language that’s new for Beta2:


·         Query operators


o   Group By, Group Join, Join, Take [While], Skip [While], Aggregate, Count, Sum, Min, Max, Average, From à Let


·         Nullable support


·         Lambda Expressions (aka Inline Functions)


·         Partial Methods – ScottGu mentioned this the other day & ScottWis posted a couple weeks back


·         Anonymous Types w/Keys – Which PaulV has already discussed


Here are a bunch of queries you can now write with VB!



‘ Find suppliers in the same city as customers


From cust In db.Customers _


Join sup In db.Suppliers _


On cust.City Equals sup.City _


Select cust.CompanyName, sup.ContactName, cust.City


 


‘ Find suppliers in the same city, with the same postalcode as customers


From cust In db.Customers _


Join sup In db.Suppliers _


On cust.City Equals sup.City And cust.PostalCode Equals sup.PostalCode


Select cust.CompanyName, sup.ContactName, cust.City


‘ Find the average unit price and count for products by category


From prod In db.Products _


Group By prod.CategoryID _


Into Average(prod.UnitPrice), Count()


 


‘ For each customer find the group of the suppliers in the same country


From cust In db.Customers _


Group Join sup In db.Suppliers _


On cust.Country Equals sup.Country _


Into SuppliersInCountry = Group _


Select cust.CompanyName, SuppliersInCountry


‘ For each customer, the count of the number of suppliers in the same country


From cust In db.Customers _


Group Join sup In db.Suppliers _


On cust.Country Equals sup.Country _


Into NumSuppliers = Count() _


Select cust.CompanyName, NumSuppliers


 


‘ Avg price, avg # in stock, and max on order for non-discontinued products


Aggregate prod In db.Products _


Where Not prod.Discontinued _


Into AvgPrice = Average(prod.UnitPrice), _


     AvgInStock = Average(prod.UnitsInStock), _


     MaxOnOrder = Max(prod.UnitsOnOrder)


‘ Find orderID, OrderDate, and OrderTotal for each order


From order In db.Orders _


Where order.OrderDate > #7/22/1996# _


Aggregate ordDet In order.Order_Details _


Into OrderTotal = _


       Sum(ordDet.UnitPrice *ordDet.Quantity * (1 – ordDet.Discount))


Select order.OrderID, order.OrderDate, OrderTotal


 


‘ Fill a dictionary with the customers by CompanyName


db.Customers.ToDictionary(Function(cust As Customer) cust.CompanyName)


 


In addition to all the new language support, we now have keyword Intellisense (finally!)


Keyword Intellisense


We have XML Intellisense! What?? That’s right.


XML Intellisense


And we’ve done some major performance improvements for the background compiler – let us know if you experience any sluggishness for this Beta.  (BTW, you should check out Scott’s post on Beta2 to avoid some problems with AJAX extensions if you’re installing to a machine that has had previous versions of Orcas installed on it or has VS 2005 side by side.)


Also, don’t forget to look for other exciting features around the product that pertain to VB and/or LINQ. For example, Linq to SQL now supports the Like operator! There’s also a new ASP:LinqDataSource control.


 


So, get the bits while they’re hot!


 


-Amanda