Time for action – one-to-many mapping
Here is the first problem:
Suppose, we have a List
of names and we want to index them as per the first two letters of these names:
- Copy the following code snippet as a query to LINQPad:
string[] firstNames = { "Samuel", "Jenny", "Joyace", "Sam" }; var map = firstNames.ToLookup(c => c.Length); foreach (var m in map) { Console.WriteLine(m.Key); foreach (var n in m) { Console.WriteLine("--" + n); } }
- Run the query and you should get the following output:
What just happened?
ToLookup()
creates what you can imagine as a one-to-many Dictionary
.
The Lambda expression c => c.Length
describes the key to be used for the ...
Get .NET 4.0 Generics now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.