Introduction
In this article, I describe how to convert a list of objects into a lookup collection. A sample project is also attached with this article.
Sometimes when we have a list of objects and we need to look up that collection by a member item in the object, we could easily benefit from converting a List collection into a Lookup collection. For example, we need to look up an entire product's inventory in a store by their Supply Date or by their price etc.
So, to get a Lookup from a list we use the extension method "ToLookup" available in a List collection's extension methods.
As per MSDN documentation
List.ToLookup(
Func<T, TKey> KeySelector,
Func<T, TElement> ElementSelector,
IEqualityComparer<TKey> Comparer
)
creates a Lookup<TKey, TElement> from an IEnumerable<T> (i.e. in this case our List) depending on the specified key selector and element selector functions.
Basically, a lookup is like a dictionary where the value associated with a key isn't one element but a sequence of elements. Lookups are generally used when duplicate keys are expected as part of normal operation.
Now we delve into the implementation of the Lookup conversion. Suppose we have a class "Product" having members (ID, Name, Price, Model, Brand, Availability, SaleItem). The class's Product declaration is as
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public string Model { get; set; }
public string Brand { get; set; }
public string Availability { get; set; }
public string SaleItem { get; set; }
public Product(int id, string name, double price, string model, string brand, string availability, string sale)
{
this.ID = id;
this.Name = name;
this.Price = price;
this.Model = model;
this.Brand = brand;
this.Availability = availability;
this.SaleItem = sale;
}
}
Then we create a list of Product objects as
List<Product> listProduct = new List<Product>()
{
new Product(1, "Mobile", 339.99, "ZZZ-B090-ZZZ-5", "Samsung", "Available", "In Sale !"),
new Product(2, "Laptop", 514.99, "RRR-111-RRR-2", "Toshiba", "Available", "Not in Sale"),
new Product(3, "Laptop", 554.99, "RRR-111-RRR-3", "Sony", "Available", "In Sale !"),
new Product(4, "Laptop", 414.99, "RRR-111-RRR-4", "HP", "Not Available", "Not in Sale"),
new Product(5, "Phone", 112.99, "AAA-22-AAA-1", "Samsung", "Not Available", "Not in Sale"),
new Product(6, "Phone", 156.99, "AAA-22-AAA-12", "Samsung", "Available", "Not in Sale"),
new Product(7, "PC", 313.69, "WWW-343-WWWW-03", "HP", "Not Available", "Not in Sale"),
new Product(8, "PC", 363.69, "WWW-343-WWW-04", "Acer", "Available", "In Sale !"),
new Product(9, "AC", 413.99, "NNN-80-NNN-342", "GE", "Not Available", "Not in Sale"),
new Product(10, "Heater", 109.99, "TTT-318-TTT-424", "Philips", "Available", "Not in Sale"),
new Product(11, "Heater", 79.99, "TTT-318-TTT-425", "GE", "Not Available", "In Sale !"),
new Product(12, "Heater", 69.99, "TTT-318-TTT-426", "Philips", "Available", "In Sale !"),
};
Now suppose we need to show the collection by their brands, then we create a Lookup by their Brands in the following way
ILookup<string,Product> lookupByBrand = listProduct.ToLookup(x => x.Brand, x => x);
Here the "x => x.Brand" portion serves as the KeySelector and the "x => x" portion serves as the ElementSelector.





Join the conversation! Your thoughts help the community grow.