ILookup<TKey, TElement> Interface

.NET Core API Documentation

Interface ILookup<TKey, TElement>

Represents a collection of elements that are indexed by a specified key type. Each key in the lookup can be associated with zero or more elements.

Summary

  • Defines a read-only collection of groups, where each group contains a key and a collection of elements associated with that key.
  • Provides efficient querying of elements based on their associated keys.
  • Typically used after a grouping operation (e.g., using LINQ's GroupBy method).

Syntax

public interface ILookup<out TKey, out TElement> : IEnumerable<ILookupGroup<TKey, TElement>>, IEnumerable

Type Parameters

TKey: The type of the keys in the lookup.

TElement: The type of the elements in the lookup.

Members

Name Description
Count Gets the number of keys in the lookup.
this[TKey key] (Indexer) Gets the collection of elements associated with the specified key.
Contains(TKey key) Determines whether the lookup contains the specified key.
GetEnumerator() Returns an enumerator that iterates through the lookup.

Remarks

The ILookup<TKey, TElement> interface is an important part of the Language Integrated Query (LINQ) framework in .NET. It provides a convenient and efficient way to work with data that has been grouped by a common key.

Unlike a Dictionary<TKey, TValue>, where each key can map to only one value, a Lookup<TKey, TElement> (the concrete implementation of ILookup<TKey, TElement>) allows a key to be associated with multiple elements. The elements for a given key are stored in an IEnumerable<TElement>.

The ILookup<TKey, TElement> interface is read-only. Once created, you cannot add or remove keys or elements.

Example

The following example demonstrates how to create and use an ILookup<int, string> from a list of strings.

using System;
using System.Collections.Generic;
using System.Linq;

public class LookupExample
{
    public static void Main(string[] args)
    {
        var words = new List<string> { "apple", "banana", "apricot", "blueberry", "cherry", "avocado" };

        // Group words by their first letter
        ILookup<char, string> groupedByFirstLetter = words.ToLookup(word => word[0]);

        Console.WriteLine($"Number of keys: {groupedByFirstLetter.Count}"); // Output: Number of keys: 3

        // Accessing elements for a specific key
        Console.WriteLine("\nWords starting with 'a':");
        if (groupedByFirstLetter.Contains('a'))
        {
            foreach (var word in groupedByFirstLetter['a'])
            {
                Console.WriteLine($"- {word}");
            }
        }
        // Output:
        // - apple
        // - apricot
        // - avocado

        Console.WriteLine("\nWords starting with 'b':");
        if (groupedByFirstLetter.Contains('b'))
        {
            foreach (var word in groupedByFirstLetter['b'])
            {
                Console.WriteLine($"- {word}");
            }
        }
        // Output:
        // - banana
        // - blueberry

        // Iterating through all groups
        Console.WriteLine("\nAll groups:");
        foreach (var group in groupedByFirstLetter)
        {
            Console.WriteLine($"Key '{group.Key}':");
            foreach (var element in group)
            {
                Console.Write($"  {element}");
            }
            Console.WriteLine();
        }
        // Output:
        // Key 'a':
        //   apple apricot avocado
        // Key 'b':
        //   banana blueberry
        // Key 'c':
        //   cherry
    }
}