using System.Collections.Generic;
public static void Main()
Customer c1 = new Customer() { ID = 1, Name = "Mike", Salary = 5000 };
Customer c2 = new Customer() { ID = 2, Name = "John", Salary = 6500 };
Customer c3 = new Customer() { ID = 3, Name = "Steve", Salary = 3500 };
List<Customer> listOfCustomers = new List<Customer>();
var dictionary = Customer.ToDictionary(x=>x.ID, listOfCustomers);
foreach(KeyValuePair<int,Customer> pair in dictionary)
Console.WriteLine($"Key: {pair.Key}, Value={pair.Value}");
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public static Dictionary<TKey, Customer> ToDictionary<TKey>(Func<Customer,TKey> filter,List<Customer> customers)
var dictionary = new Dictionary<TKey, Customer>();
foreach(var item in customers)
dictionary.Add(filter.Invoke(item),item);