Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert a List(Of KeyValuePair(Of String,Int32) into a Dictionary(Of String, Int32) using .ToDictionary
    primarykey
    data
    text
    <p>To be able to sort a dictionary by value I'm using this code: </p> <pre><code>Dim idCurrentJobs As IDictionary(Of String, Int32) = New Dictionary(Of String, Int32) 'The string in the dictionary represents a jobname and the integer is a counter for how many jobs im currently are running in the application' idCurrentJobs.Add("JobName1", 2) idCurrentJobs.Add("JobName2", 1) idCurrentJobs.Add("JobName3", 2) idCurrentJobs.Add("JobName4", 5) idCurrentJobs.Add("JobName5", 3) idCurrentJobs.Add("JobName6", 4) Dim jobsSortedByCount As List(Of KeyValuePair(Of String, Int32)) = New List(Of KeyValuePair(Of String, Int32))(idCurrentJobs) jobsSortedByCount.Sort(Function(firstPair As KeyValuePair(Of String, Int32), nextPair As KeyValuePair(Of String, Int32)) firstPair.Value.CompareTo(nextPair.Value)) idCurrentJobs = jobsSortedByCount.ToDictionary(Of List(Of KeyValuePair(Of String, Int32)))(Function(pair As KeyValuePair(Of String, Int32)) pair.Key) </code></pre> <p>The when I'm using the .ToDictionary method to convert the List object back to a Directory object I'm getting a error on the "pair.Key" saying:</p> <blockquote> <p>Value of type 'String' cannot be converted to 'System.Collections.Generic.List(Of System.Collections.Generic.KeyValuePair(Of String, Integer))</p> </blockquote> <p>How should I use the .ToDictionary to get a Dictionary object from my list of objects?</p> <p>If i change the row with the .ToDictionary method to this:</p> <pre><code>idCurrentJobs = jobsSortedByCount.ToDictionary(Of KeyValuePair(Of String, Int32))(Function(pair As KeyValuePair(Of String, Int32)) pair) </code></pre> <p>I get this error because of "Strict On":</p> <blockquote> <p>Option Strict On disallows implicit conversions from 'System.Collections.Generic.Dictionary(Of System.Collections.Generic.KeyValuePair(Of String, Integer), System.Collections.Generic.KeyValuePair(Of String, Integer))' to 'System.Collections.Generic.IDictionary(Of String, Integer)'</p> </blockquote> <p>How can i solve this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload