ToLookup in C#
A frozen one-to-many index over a sequence.
var files = new[] { "a.cs", "b.py", "c.cs", "d.md", "e.py" };
// a lookup is a frozen one-to-many dictionary
var byExt = files.ToLookup(f => Path.GetExtension(f));
Console.WriteLine(string.Join(",", byExt[".cs"]));
Console.WriteLine(string.Join(",", byExt[".py"]));
// a missing key yields an empty group, never an exception
Console.WriteLine(byExt[".txt"].Count());
How it works
ToLookup(f => extension)groups files by their suffix, once.- Indexing by key returns the group, ready to iterate.
- A missing key yields an empty group, never an exception.
Keywords and builtins used here
newstringvar
The run, in numbers
- Lines
- 10
- Characters to type
- 369
- Tokens
- 85
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 57 seconds.
Step 3 of 3 in Joins & lookups, step 9 of 17 in LINQ in depth.