ToDictionary
public static System.Collections.Generic.Dictionary<TKey,TElement> ToDictionary<TSource,TKey,TElement>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector)
public static System.Collections.Generic.Dictionary<TKey,TElement> ToDictionary<TSource,TKey,TElement>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer)
public static System.Collections.Generic.Dictionary<TKey,TSource> ToDictionary<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector)
public static System.Collections.Generic.Dictionary<TKey,TSource> ToDictionary<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey> comparer)
Description
Creates a dictionary from an enumerable collection by using the specified key selector and element selector functions.
This method uses deferred execution.
Parameters
Name | Type | Description |
---|---|---|
source |
System.Collections.Generic.IEnumerable<TSource> |
An enumerable collection of elements to create a dictionary from. |
keySelector |
Func<TSource,TKey> |
A function to extract a key from each element. |
elementSelector |
Func<TSource,TElement> |
A function to map each source element to a value. |
comparer |
System.Collections.Generic.IEqualityComparer<TKey> (optional) |
An equality comparer to hash and check keys for equality. If not specified, the default equality comparer for the key type is used. |
Returns
A System.Collections.Generic.Dictionary<TKey,TElement>
that contains keys and values from the input sequence.
Exceptions
ArgumentNullException: source
or keySelector
is null.
ArgumentException: An element in the sequence is null or some keys are duplicated.
Example
// Example usage of ToDictionary
using System;
using System.Collections.Generic;
using System.Linq;
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class Example
{
public static void Main(string[] args)
{
List<Product> products = new List<Product>()
{
new Product { Id = 1, Name = "Laptop", Price = 1200.00m },
new Product { Id = 2, Name = "Mouse", Price = 25.50m },
new Product { Id = 3, Name = "Keyboard", Price = 75.00m },
new Product { Id = 4, Name = "Monitor", Price = 300.00m }
};
// Create a dictionary where the key is the product ID and the value is the product name.
Dictionary<int, string> productNames = products.ToDictionary(p => p.Id, p => p.Name);
Console.WriteLine("Product Names Dictionary:");
foreach (var kvp in productNames)
{
Console.WriteLine($"ID: {kvp.Key}, Name: {kvp.Value}");
}
// Create a dictionary where the key is the product name and the value is the product object itself.
Dictionary<string, Product> productMap = products.ToDictionary(p => p.Name); // Uses Product object as value
Console.WriteLine("\nProduct Map Dictionary:");
foreach (var kvp in productMap)
{
Console.WriteLine($"Name: {kvp.Key}, Price: {kvp.Value.Price}");
}
}
}