How to convert LINQ query result from objects to Dictionary in C#

How to convert LINQ query result from objects to Dictionary in C#

You can convert a LINQ query result from a collection of objects to a Dictionary in C# by using the ToDictionary() extension method. Here's an example:

  • Add the following using statement:
using System.Linq; 
  • Define a sample class and create a list:
public class Person { public int Id { get; set; } public string Name { get; set; } } List<Person> people = new List<Person> { new Person { Id = 1, Name = "John" }, new Person { Id = 2, Name = "Jane" }, new Person { Id = 3, Name = "Alice" }, new Person { Id = 4, Name = "Bob" } }; 
  • Use a LINQ query to filter the data (optional) and convert the result to a Dictionary:
var filteredPeople = people.Where(p => p.Id > 1); // Optional: Filter the data // Convert the filtered result to a Dictionary Dictionary<int, Person> peopleDictionary = filteredPeople.ToDictionary(p => p.Id); // Iterate over the dictionary and print the key-value pairs foreach (var kvp in peopleDictionary) { Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value.Name}"); } 

In this example, the ToDictionary() method is called on the filteredPeople collection, with a key selector that uses the Id property of the Person class. The result is a Dictionary with integer keys (person IDs) and Person objects as values.

You can adjust the key selector and value selector according to your needs. For example, you can create a dictionary with the Name property as the key and the Id property as the value:

Dictionary<string, int> nameToIdDictionary = people.ToDictionary(p => p.Name, p => p.Id); 

Examples

  1. C# LINQ query to Dictionary conversion:

    var numbers = new List<int> { 1, 2, 3, 4, 5 }; var dictionary = numbers.ToDictionary(num => num); // or with key and value selector var dictionaryWithSelector = numbers.ToDictionary(num => num, num => num * 2); 
  2. LINQ to Dictionary conversion example in C#:

    var fruits = new List<string> { "Apple", "Banana", "Cherry" }; var dictionary = fruits.ToDictionary(fruit => fruit[0], fruit => fruit.Length); 
  3. Convert IEnumerable to Dictionary using LINQ in C#:

    var data = new List<int> { 1, 2, 3, 4, 5 }; var dictionary = data.Select(item => new { Key = item, Value = item * 2 }).ToDictionary(x => x.Key, x => x.Value); 
  4. LINQ query result to Key-Value pairs in C#:

    var fruits = new List<string> { "Apple", "Banana", "Cherry" }; var keyValuePairs = fruits.Select(fruit => new KeyValuePair<char, int>(fruit[0], fruit.Length)).ToDictionary(kv => kv.Key, kv => kv.Value); 
  5. Convert LINQ query to Dictionary with Lambda expression in C#:

    var numbers = new List<int> { 1, 2, 3, 4, 5 }; var dictionary = numbers.ToDictionary(num => num, num => num * 2); 
  6. LINQ query to Dictionary with distinct keys in C#:

    var fruits = new List<string> { "Apple", "Banana", "Cherry", "Apple" }; var dictionary = fruits.Distinct().ToDictionary(fruit => fruit[0]); 
  7. Convert LINQ query to Dictionary with multiple fields in C#:

    var persons = new List<Person> { /* List of Person objects */ }; var dictionary = persons.ToDictionary(person => person.Id, person => person.Name); 
  8. C# LINQ ToDictionary with value transformation:

    var numbers = new List<int> { 1, 2, 3, 4, 5 }; var dictionary = numbers.ToDictionary(num => num, num => $"Value: {num}"); 
  9. C# LINQ query result to Dictionary with unique keys:

    var fruits = new List<string> { "Apple", "Banana", "Cherry" }; var dictionary = fruits.ToDictionary(fruit => fruit[0], StringComparer.OrdinalIgnoreCase); 
  10. Convert LINQ query to Dictionary with grouping in C#:

    var persons = new List<Person> { /* List of Person objects */ }; var groupedDictionary = persons.GroupBy(person => person.Age).ToDictionary(group => group.Key, group => group.ToList()); 
  11. LINQ query to Dictionary with specified key and value in C#:

    var numbers = new List<int> { 1, 2, 3, 4, 5 }; var dictionary = numbers.ToDictionary(num => num, num => $"Square: {num * num}"); 
  12. C# LINQ query result to Dictionary with anonymous type:

    var numbers = new List<int> { 1, 2, 3, 4, 5 }; var dictionary = numbers.Select(num => new { Key = num, Value = $"Square: {num * num}" }).ToDictionary(x => x.Key, x => x.Value); 
  13. Convert LINQ query to Dictionary using foreach loop in C#:

    var numbers = new List<int> { 1, 2, 3, 4, 5 }; var dictionary = new Dictionary<int, string>(); foreach (var num in numbers) { dictionary[num] = $"Square: {num * num}"; } 
  14. LINQ to Dictionary conversion with ToDictionary and Select in C#:

    var numbers = new List<int> { 1, 2, 3, 4, 5 }; var dictionary = numbers.Select(num => new { Key = num, Value = $"Square: {num * num}" }).ToDictionary(x => x.Key, x => x.Value); 

More Tags

nsfilemanager assert angular-unit-test one-hot-encoding fat-free-framework nodes angular-elements marshalling pygame-surface six

More Programming Guides

Other Guides

More Programming Examples