# Monday, July 11, 2011

LINQ: Convert list to a dictionary of lists

Suppose I have a list of time cards from multiple employees, but I want to group them into dictionary, based on the Social Security Number (SSN) of the employee. Here is an example of how to convert a list of items into a dictionary of lists:

Dictionary<string, List<TimeCardInfo>> dict;
 
dict = ( from timecard in listOfTimeCards
         group timecard by timecard.SSN ).ToDictionary( x => x.Key, x => x.ToList() );
#    Comments [3] |