Easy way to Populate a Dictionary<string,List<string>>

Posted by zion on Stack Overflow See other posts from Stack Overflow or by zion
Published on 2011-02-16T15:21:47Z Indexed on 2011/02/16 15:25 UTC
Read the original article Hit count: 376

Filed under:
|

Greetings Guru's, my objective is to create a Dictionary of Lists, does a simpler technique exist?

I prefer the List(t) to IEnumerable(t) which is why I chose the Dictionary of Lists over Ilookup or IGrouping.

The code works but it seems like a messy way of doing things.

string[] files = Directory.GetFiles (@"C:\test");

Dictionary<string,List<string>> DataX = new Dictionary<string,List<string>>();

foreach (var group in files.GroupBy (file => Path.GetExtension (file)))
{
   DataX.Add (group.Key, group.ToList());
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ