Linq ToLookup Method in C#

Linq ToLookup Method in C# is used to group data in LINQ by creating a lookup table based on a key selector function. This method creates a collection of key-value pairs where the keys are the result of the key selector function, and the values are a collection of all the matching elements in the original collection.

The ToLookup() method is an extension method that can be called on any IEnumerable<T> collection. It returns an ILookup<TKey, TElement> interface, which is a read-only collection of key-value pairs.

Syntax:

ILookup<TKey, TElement> lookup = collection.ToLookup(keySelector);

Here, collection is the original collection of elements, and keySelector is the function that is used to extract the key from each element in the collection.

Let’s take a look at an example to better understand how the ToLookup() method works:

List<string> words = new List<string> { "apple", "banana", "cat", "dog", "elephant", "frog" };

ILookup<int, string> lookup = words.ToLookup(word => word.Length);

foreach (var group in lookup)
{
    Console.WriteLine("Words with length {0}:", group.Key);

    foreach (string word in group)
    {
        Console.WriteLine(word);
    }
}

Output:

Words with length 5:
apple
banana

Words with length 3:
cat
dog

Words with length 8:
elephant

Words with length 4:
frog

In this example, we have a list of words, and we are grouping them by their length using the ToLookup() method. The resulting lookup table is then iterated over using a foreach loop, with each group represented by a key-value pair. The key is the length of the words in the group, and the value is a collection of all the words in the original list with that length.

The ToLookup() method can also take an optional element selector function as a second argument, which is used to determine what values are included in the resulting lookup table. By default, the values are the entire elements of the original collection.

Syntax:

ILookup<TKey, TElement> lookup = collection.ToLookup(keySelector, elementSelector);

Here, elementSelector is the function that is used to extract the value for each key from each element in the collection.

Let’s take a look at an example that uses an element selector function:

List<Person> people = new List<Person>
{
    new Person { Name = "John", Age = 25 },
    new Person { Name = "Jane", Age = 30 },
    new Person { Name = "Alice", Age = 25 },
    new Person { Name = "Bob", Age = 30 },
};

ILookup<int, string> lookup = people.ToLookup(person => person.Age, person => person.Name);

foreach (var group in lookup)
{
    Console.WriteLine("People with age {0}:", group.Key);

    foreach (string name in group)
    {
        Console.WriteLine(name);
    }
}

Output:

People with age 25:
John
Alice

People with age 30:
Jane
Bob

In this example, we have a list of Person objects, and we are grouping them by their age using the ToLookup() method. The resulting lookup table is then iterated over using a foreach loop, with each group represented by a key-value pair. The key is the age of the people in the group, and the value is a collection of all the names of the people in the original list with that age.

In conclusion, the LINQ ToLookup method is a powerful tool for creating lookup tables that can be used to efficiently retrieve and group data based on specific keys. It allows developers to quickly and easily convert a collection into a lookup table that can be queried using standard LINQ operators. By using ToLookup, developers can streamline their code and avoid complex looping structures while still maintaining the ability to easily retrieve data based on specific keys. Overall, ToLookup is a useful method for any developer working with LINQ and collections in C#.