Converting LINQ C# Lambda Expression to VB.Net

So after many months of procrastinating I finally decided to learn LINQ.  When I first heard about LINQ I did not really see it’s true potential and the book I was reading at the time was teaching at a very deep level of the technology and the C# language that also did not click with me.  So after speaking with several friends and colleagues about what is truly the best book to learn from scratch I was directed to “Pro LINQ” by author Joe C. Rattz.  The book was written for the C# language which is usually no problem for a polyglot (thanks Abel!)  like myself.  However, in working with the examples I figured I would convert the examples, which are in C# in the book, to VB.  In doing so I ran across a conversion sample that had me searching high and low for an answer and neither Bing nor that other search engine that must not be named ;) could help me find the answer.  Several of the code converters out there couldn’t do the job either.  So in the end, I had to dig into the MSDN documentation to find my answer.

In C# there is a nice new operator for lambda expressions which looks like “ => ”.  Very readable and pretty convenient.  However, this little operator does not have an equivalent in VB.Net.  So a nice readable line in C# like:

int[] nums = numbers.Select(s => Int32.Parse(s)).OrderBy(s => s).ToArray();

in VB.Net is now:

Dim nums As Integer() = numbers.Select(Function(s As String) Int32.Parse(s)).OrderBy(Function(s As Integer) s).ToArray()

Ugly and nasty I know, but this is what needs to be done to make this LINQ expression work.  It took me a while to find this out so I figured I’d share with you.  Enjoy and happy coding!