CSharp - LINQ ToLookup

Introduction

The ToLookup operator creates a Lookup of type <K, T> from an input sequence of type T, where K is the type of the key and T is the type of the stored values.

ToLookup operator returns an object that implements the ILookup interface.

C# Lookup collection class stores values that can be retrieved with a key. Each key need not be unique, and multiple elements can be stored for a single key. Lookup using the key returns a sequence of the stored elements for that key.

Prototypes

There are four prototypes we cover. The First Prototype for the ToLookup Operator

public static ILookup<K, T> ToLookup<T, K>(
        this IEnumerable<T> source,
        Func<T, K> keySelector);

In this prototype, a Lookup of type <K, T> is created and returned.

The keySelector method extracts the key value from each input element, and that key is the element's key into the Lookup.

This version of ToLookup uses the EqualityComparer<K>.Default equality comparison class.

The second ToLookup prototype accepts an IEqualityComparer<K> object. Here is the second prototype:

public static ILookup<K, T> ToLookup<T, K>(
  this IEnumerable<T> source,
  Func<T, K> keySelector,
  IEqualityComparer<K> comparer);

This prototype uses an IEqualityComparer object to compare the key value.

It will use this comparer object to compare the key to the keys in the Lookup to determine whether there is a match.

The third ToLookup prototype can return Lookup of a different type than the input sequence element. Here is the third prototype:

public static ILookup<K, E> ToLookup<T, K, E>(
  this IEnumerable<T> source,
  Func<T, K> keySelector,
  Func<T, E> elementSelector);

Through the elementSelector argument, you can return the portion of the input element-or a newly created object.

The fourth prototype for the ToLookup operator can accept an elementSelector and a comparer object. Here is the fourth prototype:

public static ILookup<K, E> ToLookup<T, K, E>(
  this IEnumerable<T> source,
  Func<T, K> keySelector,
  Func<T, E> elementSelector,
  IEqualityComparer<K> comparer);

Exceptions

ArgumentNullException is thrown if the source, keySelector, or elementSelector argument is null or if a key returned by keySelector is null.

The following example shows how to return a lookup by calling the ToLookup operator.

First we create the Lookup using the Actor.birthYear member as the key into the Lookup.

Next we index into the Lookup using our key, 1964.

Then we enumerate through the returned values.

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*w  w  w.jav a 2  s . c  o  m*/
{
    static void Main(string[] args)
    {

        ILookup<int, Actor> lookup = Actor.GetActors().ToLookup(k => k.birthYear);

        //  Let's see if we can find the 'one' born in 1964.
        IEnumerable<Actor> actors = lookup[1964];
        foreach (var actor in actors)
            Console.WriteLine("{0} {1}", actor.firstName, actor.lastName);

    }
}
public class Actor
{
    public int birthYear;
    public string firstName;
    public string lastName;

    public static Actor[] GetActors()
    {
        Actor[] actors = new Actor[] {
           new Actor { birthYear = 1964, firstName = "Kotlin", lastName = "Ruby" },
           new Actor { birthYear = 1968, firstName = "Owen", lastName = "Windows" },
           new Actor { birthYear = 1960, firstName = "Javascript", lastName = "Spader" },
           new Actor { birthYear = 1964, firstName = "Scala", lastName = "Java" },
         };

        return (actors);
    }
}

Result

Related Topics