LINQ query to return a Dictionarystring, string
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you already have a sequence in C#, the simplest way to build a Dictionary<string, string> is usually ToDictionary. The real work is deciding which property becomes the key, which value to store, and how to handle duplicates or missing data.
Using ToDictionary Correctly
ToDictionary projects each element in a sequence into a key-value pair. It is eager, which means it runs immediately and throws if keys are duplicated.
Consider a list of users where we want a lookup from username to email address:
The first lambda chooses the key, and the second chooses the value. If the source element is already a key-value shaped object, this is often all you need.
Query Syntax Versus Method Syntax
LINQ query syntax is useful for filtering or joining, but dictionaries are still created with a terminal method call. For example:
That means the answer is rarely a pure query expression. Usually, you compose a query and finish with ToDictionary.
Handling Duplicate Keys
Duplicate keys are the biggest trap. This throws an exception:
If duplicates are possible, decide the rule explicitly. One common option is grouping and then selecting the first or last item.
That version states the conflict policy instead of failing unpredictably at runtime.
Creating A Dictionary From Pairs
If you already have tuples or anonymous projections, converting them is still straightforward.
You can also normalize data as part of the projection:
That is often cleaner than building the dictionary first and then mutating it.
Case-Insensitive Dictionaries
Sometimes you want key lookups to ignore case. ToDictionary has an overload that accepts an equality comparer.
Choosing the comparer at creation time is better than forcing every caller to normalize keys manually.
When Not To Use ToDictionary
If the source sequence may contain multiple values per key and you need to keep them all, a dictionary of lists is more appropriate. In that case, use GroupBy and materialize collections.
If you only need one pass and no random access, materializing a dictionary may also be unnecessary overhead.
Common Pitfalls
The most common failure is duplicate keys. ToDictionary does not silently overwrite entries, so it will throw ArgumentException as soon as a repeated key appears.
Another issue is using a nullable property as the key without validating the data source. Null keys are invalid for many dictionary scenarios and usually signal a modeling problem upstream.
Developers also sometimes overuse query syntax for simple projections. A direct method chain is usually clearer when the only goal is to build a dictionary.
Finally, be deliberate about case sensitivity. Usernames, headers, and environment variables often have domain-specific comparison rules. Use the right comparer instead of assuming the default behavior is correct.
Summary
- Build a
Dictionary<string, string>from a sequence withToDictionary(keySelector, valueSelector). - '
ToDictionaryexecutes immediately and throws if keys are duplicated.' - Use
GroupByfirst when duplicates are possible and you need a conflict rule. - Pass a comparer such as
StringComparer.OrdinalIgnoreCasewhen key matching should ignore case. - Materialize a dictionary only when you actually need keyed lookup behavior.

