LINQ
C#
Dictionary
Programming
.NET

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.

Practice algorithms

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:

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5var data = new Dictionary<int, string>
6{
7    [1] = "one",
8    [2] = "two",
9    [3] = "three"
10};
11
12var keys = data.Select(pair => pair.Key).ToList();
13var values = data.Select(pair => pair.Value).ToList();
14
15Console.WriteLine(string.Join(", ", keys));
16Console.WriteLine(string.Join(", ", values));

Inside the lambda, pair is a KeyValuePair<int, string>.

Project Into Another Shape

You can also select into anonymous objects or tuples:

csharp
1var projected = data.Select(pair => new
2{
3    Id = pair.Key,
4    Label = pair.Value.ToUpperInvariant()
5}).ToList();

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:

csharp
1var upper = data.ToDictionary(
2    pair => pair.Key,
3    pair => pair.Value.ToUpperInvariant()
4);

Or combine Select and ToDictionary explicitly:

csharp
var transformed = data
    .Select(pair => new { NewKey = pair.Key * 10, NewValue = pair.Value.Length })
    .ToDictionary(x => x.NewKey, x => x.NewValue);

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:

csharp
1var longValues = data
2    .Where(pair => pair.Value.Length > 3)
3    .Select(pair => pair.Value)
4    .ToList();

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:

csharp
1if (data.TryGetValue(2, out var value))
2{
3    Console.WriteLine(value);
4}

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 Select on a dictionary to return another dictionary automatically.
  • Forgetting that the lambda parameter is a KeyValuePair<TKey, TValue>.
  • Using LINQ for direct keyed lookup when TryGetValue would be clearer and faster.
  • Creating duplicate keys during ToDictionary, which throws an exception.

Summary

  • LINQ Select on a dictionary iterates KeyValuePair<TKey, TValue> items.
  • Use Select when you want to project keys, values, or another output shape.
  • Use ToDictionary when the final result should be another dictionary.
  • Combine Where and Select for filtered transformations.
  • Prefer direct dictionary lookup methods for single-key access instead of LINQ.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.