Select a DictionaryT1, T2 with LINQ
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 use LINQ Select on a Dictionary<TKey, TValue>, each element you receive is a KeyValuePair<TKey, TValue>. That means you can project keys, values, anonymous objects, tuples, or even build a new dictionary, depending on what shape you actually want.
The important point is that Select does not automatically return another dictionary. It returns a projected sequence. If you want a new dictionary at the end, you usually need ToDictionary.
Select Keys or Values
A dictionary is enumerable, so LINQ works directly on it:
Inside the lambda, pair is a KeyValuePair<int, string>.
Project Into Another Shape
You can also select into anonymous objects or tuples:
This is useful when you want to pass dictionary entries into a UI model, export format, or API response shape.
The key idea is that Select transforms each entry one by one. It does not care that the original source happens to be a dictionary. You can build exactly the output model you need without changing the original collection first.
Build a New Dictionary
If your goal is another dictionary rather than a sequence, use ToDictionary after projection:
Or combine Select and ToDictionary explicitly:
This is a common source of confusion. Select changes shape, but ToDictionary is what actually creates the new dictionary container.
Filter Before Selecting
LINQ becomes especially useful when you combine Where and Select:
This reads naturally: filter the dictionary entries, then project the part you want.
When Direct Access Is Better
Not every dictionary operation should go through LINQ. If you already know the key you need, direct dictionary lookup is faster and clearer:
Use LINQ when you want to transform or query the whole sequence of entries. Use dictionary methods when you want keyed access.
Duplicate Keys Matter
If you build a new dictionary with ToDictionary, the generated keys must be unique. If two projected elements produce the same key, ToDictionary throws an exception. That is one reason it is useful to think about the projection result separately from the final collection type.
In other words, Select is very forgiving, but dictionary construction is not.
Common Pitfalls
- Expecting
Selecton a dictionary to return another dictionary automatically. - Forgetting that the lambda parameter is a
KeyValuePair<TKey, TValue>. - Using LINQ for direct keyed lookup when
TryGetValuewould be clearer and faster. - Creating duplicate keys during
ToDictionary, which throws an exception.
Summary
- LINQ
Selecton a dictionary iteratesKeyValuePair<TKey, TValue>items. - Use
Selectwhen you want to project keys, values, or another output shape. - Use
ToDictionarywhen the final result should be another dictionary. - Combine
WhereandSelectfor filtered transformations. - Prefer direct dictionary lookup methods for single-key access instead of LINQ.
Related reading
- Select combination of elements from array whose sum is smallest possible positive number
- Select k random elements from a list whose elements have weights
- SELECT Specific Value from map
- Select top N elements of related objects
- Select folder dialog WPF
- SELECT FROM X WHERE id IN ... with Dapper ORM
- Selecting a range of items inside an array in C
- Selection algorithms on sorted matrix

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.