Jonathan Eilath
Jonathan Eilath

Reputation: 45

Converting LINQ to dictionary in vb.net

I'm trying to convert LINQ to dictionary in vb.net, but can't seem to understand why I get all these exceptions thrown at me.

All I'm trying to do, is to sort by descending value with help from LINQ.

Here's my code:

'Declaring my primary dictionary    
Dim nameValDict As New Dictionary(Of Object, Object)

... nameValDict is now getting filled up with data

'Declaring temporary sorted dictionary with LINQ
Dim sortedDict = (From entry In nameValDict Order By entry.Value Descending Select entry)

'Replace with sorted results from LINQ
nameValDict = sortedDict.ToDictionary(Function(x) x.Key, Function(x) x.Value)

Exception: System.ArgumentException (Object should be of type Double) - On last line.

I'll appreciate ANY help.

Upvotes: 2

Views: 1586

Answers (1)

Jens Kloster
Jens Kloster

Reputation: 11277

Its properly your Order By in this statement:
From entry In nameValDict Order By entry.Value Descending Select entry

Becuase entry.Value is of type Object, and you can't sort desending on Object. The program doesnt know how to compare Objects against eachother.

Upvotes: 1

Related Questions