Thursday, November 08, 2007

VB Linq Cookbook

The VB Team has a great series named Linq Cookbook. In it they show some examples of Linq usage in VB that look great.

Entries so far:

linq cookbook recipe 1 change the font for all labels on a windows form

linq cookbook recipe 2 find all capitalized words in a phrase and sort by length then alphabetically

linq cookbook recipe 3 find all the prime numbers in a given range

linq cookbook recipe 4 find all complex types in a given assembly

linq cookbook recipe 5 concatenating the selected strings from a checkedlistbox

linq cookbook recipe 6 your first linq application using northwind

linq cookbook recipe 7 selecting pages of data from northwind

linq cookbook recipe 8 querying xml using linq

linq cookbook recipe 9 dynamic sort order

linq cookbook recipe 10 pre compiling queries for performance

And apparently VB support for Linq seems superior to C#. I had already read about it (VisualBasic Envy), but the XML syntax seems much terser and cleaner. Just compare:

Dim itemList1 = 
      From item In rss.<rss>.<channel>.<item> _
          Where item.<desc>.Value.Contains("LINQ") Or _
               item.<title>.Value.Contains("LINQ")

http://blogs.msdn.com/vbteam/archive/2007/10/01/linq-cookbook-recipe-8-querying-xml-using-linq.aspx

to

var query = from c in xElement.Descendants("book")
     where double.Parse(c.Element("price").Value) < 10
     select new {
            author = c.Element("author").Value,
            title =   c.Element("title").Value
    };

http://blog.adrianroman.ro/post/C-30-Querying-XML-in-C-with-LINQ-to-XML.aspx

 

Update:

Just found out an example of Linq usage inside Excel (with VSTO). The video is available at: How Do I: Use LINQ Against Excel Ranges?

Looking at the example, two things are missing (that could be implemented):

  • Turning an excel range into a list of objects (better yet, mapping the columns into fields of a supplied class)
  • "Dumping" the results starting on a specified cell 

Other than that, just another great example of Linq usage.

 

Technorati tags: , , ,

3 comments:

Mário Romano said...

I'm not sure about the better support for LINQ on VB. In my opinion VB has chosen to change it's grammar in order to support some kind of XML embedding, as C# has chosen not to "pollute" the language with an XML specific detail.

André Cardoso said...

I disagree with the "pollute" part.
What is Linq, if not a change in .NET languages grammar to better support data querying?
I think the approach is much cleaner (as is Linq), than "polluting" the code with implementation details :)

If we chose to stick with "non polluting" changes, we'd still be using .NET strings ;)
Or fall into the Java trap - don't make any breaking changes or evolutions because the language is mature/stagant (I know Java is planning to incorporate some changes, but they still seem very afraid of any major change - see generics, for example).

This comment is almost longer than my original post. Better stop now.

Unknown said...

We are talking about the archetypal matte Chanel Wallet
that is fabricated. The alligator Chanel handbags
has CC flaps. Alligator derma is admired as the toughest actual you can use for a replica Chanel handbags
.


Bentley GT
has consistently been accepted for its ultimate superior and accepted in accomplishment superb designs of watches. Breitling Aeromarine
is a blazon of watch which has been advised to clothing the requirements of pilots. Go to a acclaimed retail abundance of Breitling Windrider
and boutique to abroad for your new Bentley 6.75
.