Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant way to go from list of objects to dictionary with two of the properties

i seem to write this code over and over again and wanted to see if there was a better way of doing it more generically.

I start out with a list of Foo objects

Foo[] foos = GenerateFoos(); 

I think want to create a dictionary where the key and value are both properties of Foo

for example:

Dictionary<string, string> fooDict = new Dictionary<string, string>(): foreach (Foo foo in foos) {     fooDict[foo.Name] = foo.StreetAddress; } 

is there anyway of writing this code generically as it seems like a basic template where there is an array of objects, a key property a value property and a dictionary.

Any suggestions?

I am using VS 2005 (C#, 2.0)

like image 202
leora Avatar asked Jun 01 '09 11:06

leora


People also ask

How we convert the list into dictionary?

To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type.

How to convert a list to a dictionary Python?

To convert two lists to a dictionary in Python, use the zip() function, and it returns the list of tuples and passes that list of tuples to a dict() function, and it returns the dictionary.

Can a dictionary be a property C#?

Item[] Property. This property is used to get or set the value associated with the specified key in the Dictionary. Here, key is the Key of the value to get or set.

How do you create a dictionary from a list?

The first list of items will be keys for the dictionary, and the second list of items will be the dictionary. The zip (fields, values) method returns an iterator that generates two-items tuples. If you call dict () on that iterator, you can create the dictionary you need.

Is it possible to Zip A Dictionary?

One important thing to note that dictionary = {zip (keys, values)} will not work. You have to declare as dict (zip ()) explicitly. If you make it work, you have to use proper dictionary comprehension.

How to create a dictionary from two sequences in Python?

To create a dictionary from two sequences, use the dict () and zip () method. The dict (zip (keys, values)) needs the one-time global lookup each for dict and zip. Still, it doesn’t create any needless intermediary data structures or deal with local lookups in function applications.


2 Answers

With LINQ:

var fooDict = foos.ToDictionary(x=>x.Name,x=>x.StreetAddress); 

(and yes, fooDict is Dictionary<string, string>)


edit to show the pain in VS2005:

Dictionary<string, string> fooDict =     Program.ToDictionary<Foo, string, string>(foos,         delegate(Foo foo) { return foo.Name; },         delegate(Foo foo) { return foo.StreetAddress; }); 

where you have (in Program):

public static Dictionary<TKey, TValue> ToDictionary<TSource, TKey, TValue>(     IEnumerable<TSource> items,     Converter<TSource, TKey> keySelector,     Converter<TSource, TValue> valueSelector) {     Dictionary<TKey, TValue> result = new Dictionary<TKey, TValue>();     foreach (TSource item in items)     {         result.Add(keySelector(item), valueSelector(item));     }     return result; } 
like image 130
Marc Gravell Avatar answered Sep 22 '22 01:09

Marc Gravell


If you are using framework 3.5, you can use the ToDictionary extension:

Dictionary<string, string> fooDict = foos.ToDictionary(f => f.Name, f => f.StreetAddress); 

For framework 2.0, the code is pretty much as simple as it can be.

You can improve the performance a bit by specifying the capacity for the dictionary when you create it, so that it doesn't have to do any reallocations while you fill it:

Dictionary<string, string> fooDict = new Dictionary<string, string>(foos.Count): 
like image 41
Guffa Avatar answered Sep 22 '22 01:09

Guffa