LINQ - Convert List to Dictionary with Value as List
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
When you want a dictionary whose values are lists, the usual LINQ pattern is to group the source items by key and then project each group into a list. In other words, this is usually a grouping problem, not a plain ToDictionary problem. Once that is clear, the code becomes straightforward and readable.
The Core Pattern: GroupBy Then ToDictionary
Suppose you have records with a key and a value.
That produces a dictionary where each unique key maps to a list of the associated values.
Why ToDictionary Alone Is Not Enough
A direct ToDictionary(x => x.Key, x => x.Value) only works when keys are unique. If the source contains repeated keys, it throws an exception.
That fails because A and B appear more than once. Grouping is the correct model when duplicate keys are expected.
Keeping Whole Objects Instead of Projected Values
Sometimes you do not want a Dictionary<TKey, List<TValue>>. You want a Dictionary<TKey, List<TItem>>.
This is useful when later logic needs the full object rather than just one property.
A Small Reusable Helper
If you do this often, a helper method can keep the call sites clean.
Then use it like this:
This keeps the intent obvious without repeating the grouping pipeline everywhere.
Preserve Ordering Carefully
The lists inside the dictionary preserve the order in which elements appear within each group based on the original sequence. If you need sorting, apply it before or after grouping.
For example, sort each value list descending:
This is often useful when the grouped values will be displayed or processed in a specific priority order.
Use Lookup if Read-Only Grouping Is Enough
If you only need grouped access and do not specifically require a dictionary, ToLookup is another option.
A lookup is convenient for read-only grouped access, but it is not the same as a mutable dictionary of lists.
Common Pitfalls
- Using
ToDictionarydirectly when duplicate keys exist in the source. - Forgetting that grouping is the real operation when each key should map to multiple values.
- Projecting the wrong thing inside the group and ending up with full objects when only values were needed, or vice versa.
- Assuming ordering is automatic beyond the original sequence when later sorting rules actually matter.
- Using a dictionary when a lookup would be simpler for read-only access.
Summary
- If each key should map to multiple values, use
GroupBybeforeToDictionary. - A direct
ToDictionaryworks only when keys are unique. - Project grouped items into
ToList()to produce dictionary values as lists. - Keep full objects or projected values depending on what the rest of the code needs.
- Use
ToLookupwhen grouped read-only access is enough and mutability is unnecessary.
Related reading
- LINQ Determine if two sequences contains exactly the same elements
- LINQ query to return a Dictionarystring, string
- List comprehension Returning two or more items for each item
- List comprehension vs. lambda + filter
- LINQ - Full Outer Join
- LINQ - Left Join, Group By, and Count
- List comprehension vs. lambda filter
- List comprehension vs map

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.