Using .ToDictionary()

Viewed 120931

I have a method returning a List, let's call it GetSomeStrings().

I have an extension method on string class, returning number of characters in the string, eg. myString.Number('A').

I would like to, in a single line, grab a dictionary. An entry of the dictionary contains the string, and the number of a chosen character in the string.

Actually I do the following:

var myDic = GetSomeStrings().ToDictionary(x=>x.Number('A'));

which gives me a dictionary <int,string>; I would like the key as the string.

After, I'd like to order the dictionary on the int value. It is possible to include this in the previous statement ?

I'd really like to avoid a collection enumeration to sort or create the dictionary from the list, which is what i do actually without troubles. Thank you for your optimization help !

7 Answers
var users = this.context.ad_user.ToDictionary(v => v.DistinguishedName,
v => new ad_user
{
     DistinguishedName = v.DistinguishedName,
     CN = v.CN,
     DisplayName = v.DisplayName,
     Info = v.Info,
     Mail = v.Mail
 });
Related