Click here to Skip to main content
15,938,848 members
Articles / Programming Languages / C# 3.5

Using Linq to create a Dictionary of sub-Lists by grouping from a collection.

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
27 May 2014CPOL2 min read 64.9K   8   7
This is really easy - but it's hard to work out for the first time. For example, get a list of files, and group them by the date which is in the 2nd to 10th characters of the name, ignoring extensions. Easy!


I had a problem where I was passed an array of paths to files, and I had to group them together according to a portion of the file name. Let's say they were log files from a number of subsystems, and so they all had different prefixes, then the date in ISO format, then some more text (sometimes) and an extension which was often - but not always - ".log". Needless to say, the list isn't sorted by anything particularly useful...


Now, group them together so that you can process all of todays files as one group, yesterdays as a different one, and so forth.

20140525      D:\Logs\AB20140525Dx3.log
20140526      D:\Logs\AB20140526ELM.log, X:\updates\XH20140526.txt
20140527      D:\Logs\AB20140527Dx3.log, X:\updates\Logs\AB20140527ELM.log, C:\XH20140527.txt

It should be simple, with Linq - and it is.


The problem is not working out what to do - that's obvious, a Dictionary<string, List<string>> is perfect - but finding out exactly how to do it took me far, far too long - hence this little tip.

Using the code

Dictionary<string, List<string>> groups = rawFiles.GroupBy(r => GetDateFromPath(r)).ToDictionary(g => g.Key, g => g.ToList());

You need a "working" method to extract the info you want, but in this case that is trivial:

private string GetDateFromPath(string path)
    return Path.GetFileNameWithoutExtension(path).Substring(2, 8);

A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.

So, how does it work?

Dictionary<string, List<string>> groups

Somewhere to put the results

rawFiles.GroupBy(r => GetDateFromPath(r))

Group the input paths by just the info we are interested in - the date info becomes the dictionary Key, and the Group contains the paths that have the same date info.

.ToDictionary(g => g.Key, g => g.ToList());

Ah! Magic! :laugh: ToDictionary is what caused me grief, because the MSDN documentation is very poor, and the examples I found on the internet didn't quite work - probably because they were for slightly different purposes and I misinterpreted what they actually did.

ToDictionary takes two parameters, and converts the group (or other enumerable) into the values.

The first parameter is a key (which we obviously get from the Groups key we created earlier) and teh section is so damn obvious I can't believe it took me that long to work out...just convert the Group to a List...simple, isn't it? Once you figure out that both parameters need a lambda, and that one of them needs ToList anyway... :doh:


Original version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

BugJust like your profile says... Pin
Brisingr Aerowing24-Mar-15 13:06
professionalBrisingr Aerowing24-Mar-15 13:06 
GeneralRe: Just like your profile says... Pin
OriginalGriff24-Mar-15 23:19
mveOriginalGriff24-Mar-15 23:19 
QuestionAwesome OG Pin
Garth J Lancaster9-Feb-15 15:37
professionalGarth J Lancaster9-Feb-15 15:37 
AnswerRe: Awesome OG Pin
OriginalGriff9-Feb-15 21:49
mveOriginalGriff9-Feb-15 21:49 
Question2 things Pin
_Noctis_12-Aug-14 3:18
professional_Noctis_12-Aug-14 3:18 
AnswerRe: 2 things Pin
OriginalGriff12-Aug-14 4:32
mveOriginalGriff12-Aug-14 4:32 
GeneralRe: 2 things Pin
_Noctis_12-Aug-14 12:47
professional_Noctis_12-Aug-14 12:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.