C # Linq Result Book Reference

I have a lambda expression that gets results from a Dictionary.

var sortedDict = (from entry in dctMetrics 
                  orderby entry.Value descending 
                  select entry);

The expression returns the pairs that I need, I see them in the IDE debug mode.

How do I return a dictionary of the same type as the source? I know that sortedDict TElement is KeyValuePair, but it's hard for me to understand the syntax of the ToDictionary extension method. I also tried to force the var result to piecewise build a new dictionary, but to no avail.

Is there something like this (functionality wise):

var results = (from entry in dictionary 
               orderby entry.Value descending 
               select entry);
Dictionary<string,float> newDictionary = results as (Dictionary<string,float>);
+5
source share
1 answer

You can do it as follows:

var newDictionary = results.ToDictionary(r => r.Key, r => r.Value);

, " , , ".

, , , Dictionary<T, U> -, , . SortedDictionary SortedList, .

+15

All Articles