I’ve been using Linq to Objects for quite some time - so it was quite a surprise to me when I discovered something new recently - the LookUp class.

LookUp is much like the more familiar Dictionary class, but with a few differences that make it easier to use with Linq.

  • Doesn’t require unique keys - there can be multiple instances with the same key. When you lookup a key, the result is a sequence of values;

  • If you index with a key that isn’t there, you get a sequence with zero elements, not an exception.

  • Immutability - an instance of LookUp is created fully formed, with all instances, by the Enumerable.ToLookup() factory extension method.

It seems to me that LookUp addresses the major deficiencies of the Dictionary class, giving us a better tool to use. I know I’ll be making frequent use of LookUp in my own code.

Comments

blog comments powered by Disqus
Next Post
A Great Upgrade Experience  13 Feb 2010
Prior Post
Improving on POCO: The ORM Problem  04 Feb 2010
Related Posts
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
Archives
February 2010
2010