Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write this lambda select method in VB.net?

For I've tried this:

Dim exampleItems As Dictionary(Of String, String) = New Dictionary(Of String, String) Dim blah = exampleItems.Select (Function(x) New (x.Key, x.Value)).ToList 'error here 

But I'm getting a syntax error and all the examples that I've seen are in C#.

like image 944
dotnetN00b Avatar asked May 22 '12 18:05

dotnetN00b


People also ask

What is a lambda expression vb net?

A lambda expression is a function or subroutine without a name that can be used wherever a delegate is valid. Lambda expressions can be functions or subroutines and can be single-line or multi-line. You can pass values from the current scope to a lambda expression.


1 Answers

This would be:

Dim blah = exampleItems.Select (Function(x) New With { .Key = x.Key, .Value = x.Value }).ToList  

For details, see Anonymous Types. (Depending on usage, you might also want Key or Value to be flagged with the Key keyword.)

That being said, Dictionary(Of TKey, Of TValue) already is an IEnumerable(Of KeyValuePair(Of TKey, Of TValue), so you can also just do:

Dim blah = exampleItems.ToList 

And you'll have a list of KeyValuePair, which has a Key and Value property already. This really means there's no need to make the anonymous type.

like image 172
Reed Copsey Avatar answered Sep 20 '22 09:09

Reed Copsey