Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Enumerable.ToLookup: practical reference

Enumerable.ToLookup: practical reference: Immediately builds a read-only index where one key can have multiple values.

Version: .NET LINQ; verified on .NET 10.0.14

Syntax

source.ToLookup(/* overload-specific arguments */)

Immediately builds a read-only index where one key can have multiple values.

Use it for repeated multivalue lookups.

The lookup does not update when the source changes. Its comparer is fixed at construction.

A missing key returns an empty sequence instead of throwing; a null source or selector causes ArgumentNullException.

The complete program actually uses Enumerable.ToLookup; its exact expected stdout is 2.

Examples

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

var lookup = new[] { "ant", "ape", "bee" }.ToLookup(word => word[0]);
Console.WriteLine(lookup['a'].Count());

Result

2

The complete program actually uses `Enumerable.ToLookup`; its exact expected stdout is `2`.

Sources