views:

41

answers:

2

I'm trying to translate some C# LINQ code into VB.NET and am stuck on how to declare an anonymous type in VB.NET.

.Select(ci => 
    new { CartItem = ci, 
          Discount = DiscountItems.FirstOrDefault(di => di.SKU == ci.SKU) }) 

How do you translate C#'s new { ... } syntax into VB.NET?

+2  A: 

Anonymous Types in the MSDN library

Dänu
@Robert Harvey - You have to make sure VB is the selected language in the tabs.
Justin Niessner
@Justin: Never mind. I switched to script-free view, and the problem went away.
Robert Harvey
+2  A: 

new { ... } becomes

New With { ... } in VB.NET

or

New With {Key ... } if you want to use Key properties (allows you to compare two anonymous type instances but does allow the values of those properties to be changed).

So I'm guessing your statement would look like:

.Select(Function(ci) New With {Key 
    .CartItem = ci,
    .Discount = DiscountItems.FirstOrDefault(Function(di) di.SKU = ci.SKU)
})
Justin Niessner