IDictionary filter (from TK, TV) to IDictionary (from TK, TV) with linq
Something very basic seems to elude me.
Dim foo As New Dictionary(Of String, String)
foo.Add("key", Nothing)
foo.Add("key2", "something")
I want to get IDictiorany (Of String, String) back, only with items that have a non-empty value. I thought this would do:
foo.Where(Function(x) Not String.IsNullOrEmpty(x.Value))
But it ends up with the wrong type. Adding:
.ToDictionary(Function(x) x.Key)
Does not help. Any advice?
+1
source to share
1 answer
Ah ... Was answering my own question. Leave it just in case it is helpful to someone else.
Dim foo As Dictionary(Of String, String)
foo.Add("k1", Nothing)
foo.Add("k2", "something")
Dim IDictionary(Of String, String) res = foo _
.Where(Function(x) Not String.IsNullOrEmpty(x.Value)) _
.ToDictionary(Function(x) x.Key, Function(y) y.Value)
+2
source to share