Some people are not sure of the difference between ToLookup vs ToDictionary LINQ methods.
To break it down real simple:
- One is a look up (ToLookup)
- One is a dictionary (ToDictionary)
Duh. That’s what you’re probably thinking. The real difference is understanding what each of these data structures do.
Lookups
A lookup is a key value pair in which multiple keys of the same value can exist, therefore resulting in a “lookup” type of scenario. The perfect example for this is to assume that we have a List of Author objects. The author object contains the following:
- First Name
- Last Name
- Year their first book was published
- Social Security Number
Assume that we’d like to give the user the option to select the authors based upon the year they published their first book in a WPF app. We could do that like this:
// Returns a list of Authors var authors = authorService.FindAll(); // Turn the list into a lookup for later use var authorLookup = authors.ToLookup(k => k.YearFirstPublished, v => v); // Further down in the code // Find all authors who published their first book in 1969 // Returns an enumerable of Authors that match that lookup value var authorsPublishedInParticularYear = authorLookup[1969];
This will return all authors who published their first book in 1969. We could use this look up later in the application as well to look up other values.
The key thing to note here is that a Lookup can have multiple keys of the same value.
Dictionaries
A dictionary is a key value pair in which a key can only exist once within the dictionary. You cannot have two entries with the same key. This is the difference between the Dictionary and Lookup data structures.
Using the same Author object we described above we can find an author by their Social Security Number if we turned the list into a dictionary for fast lookups based upon a unique key (the social security number).
This will return a single Author object who’s social security number matches ‘555-55-5555’.
// Returns a list of Authors var authors = authorService.FindAll(); // Turn the list into a lookup for later use var authorDictionary = authors.ToDictionay(k => k.SocialSecurityNumber, v => v); // Further down in the code // Finds the author with 555-55-5555 as their Social Security Number var author = authorDictionary["555-55-5555"];
Conclusion
While dictionaries and lookups seem to be nearly the same data structure, they provide completely different functionality based upon the value the key possesses.
KingWhitey says
So a ToLookup is similar to a NameValueCollection?
Justin Chase says
So what's the difference between a lookup and a hashtable?
Ross says
@Justin Chase: A lookup has a little more plumbing to it. Lookup(of TKey, TValue) is essentially Hashtable(ofTKey, IEnumerable(Of TValue)).
Justin Chase says
I see, but multiple keys can point to the same value? Sort of a reverse hashtable?
data recovery says
Does it supports Array with Oops?
ugg boots says
Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
Jordan Pro Classic
If its common it might be difficult to get, however, be creative and you can usually find something.
Bani Pe Net says
nice script you put up. very usefull
Xx says
Shouldn't a lookup be described as having multiple values for the same key rather than multiple keys of the same value?
NavinKumar.K.S says
Nice Article Donn i am NavinKumar from India. i like to know many things like this concept can you please help me
NavinKumar.K.S says
Hi Donn,
i like this article its is very useful.
Arvind B says
Yes, you’re correct. Since each key has a IEnumerable<tvalue> as it’s value.
Arvind B says
There’s no reason why multiple keys ‘shouldn’t’ point to the same object.
Stevehalennss says
This article is very interesting and more useful and I like this article very much. The difference linq tolookup vs todictionary is massive..
————
Stevehalen
DNS Lookup
custom writing help says
I think the mapping is done by defining classes that correspond to the tables in the database, and containing all or a subset of the columns in the table as data members
Toaihv says
Great post. Many thanks !!!
haha says
replica designer bags I recommend the package
replica designer handbags Of inexpensive package
air max 2012 Comfortable shoes
nike shox turbo Cheap shoes
men puma shoes Unique design Shoes
air max 90 Variety of shoe styles
wholesale puma shoes Pretty shoes
puma shoes sale Cheap comfortable shoes
timberland mens boots Discount a lot of
gucci women shoes Quite well shoes
louis vuitton outlet Very nice
Coach Factory Online says
Business coaching is an informal, open affair. You will meet with the coach and he will assess your business’s needs, and then tailor his services to your precise requirements.
Coach Factory Outlet says
http://buycoachfactoryoutletsz.com
Coach Factory Outlet says
http://buycoachfactoryoutletsz.com
Yuehuosudou Sale says
They’re not chosen, so one of these ideal for you. For more flexibleness a lot more like these, there are lots of discount ‘shoulder’ variations outlet louis vuitton uk. The louis vuitton uk are built around the idea of luxury travel, so it follows naturally that it would be particularly suited to a season that came about out of the need for pre-season travel goods for those who have the money and flexibility to escape the winter weather in less exotic locales. amazing beats by dre for those busy days or nights of perfect. headphones handbags outlet fashion handbags and signature characteristics and use of quality fabrics materials. Come to dr dre beats and find brand new beats Factory Shoes Ivory Gray here, which are one of the casual, leisure yet uncommon beats Factory shoes for women on sale. ralph lauren uk is so elegance and perfect that I cant help purchasing one. They are good quality with competitive price and the symbol of elegance. customers are not only interested in handbags and purses, but also fond of?shoes and jewelry which is sold at the coach outlet. Coach is well known for being one of the few American high-end leather goods brands. Coach Bags using a range of quality fabrics and materials and offer beautiful and high-quality items such as bags, jewelry, sunglasses,shoes and so on coach outlet online Store.. coach outlet has become necessary in everyday life of women seeking fashion and simple designs simple and elegant to look at very affordable and practical.
Tomasz Wesołowski says
Did you mean: “A lookup can have multiple values with the same key”?
GoRoS says
I agree with you Tomasz
Tim Schmelter says
It’s All A Matter Of Point of View. It’s important to note that you can
input a lookup with duplicate keys, but that doesn’t mean that the lookup
contains duplicates afterwards. It’s similar to a collection where every
value of a duplicate key will be added. So you end with one unique key
and a sequence of values which can be empty(would also cause an
exception in a dictionary) or or contains one or multiple values.
abatishchev says
Agree. It’s worth to update the post. Please.
jokjojjokj says
Do you really mean ToLookUp allows to have multiple values associated with one key, or one key associated with multiple values, which ToDictionnary cannot allow?
DisqusTech says
I love the example…. I dislike the “var” word for examples………it’s much harder to follow
ILookup authorLookup = authors.ToLookup(k => k.YearFirstPublished, v => v);
IEnumerable authorsPublishedInParticularYear = authorLookup[1991];
var authorDictionary = authors.ToDictionary(k => k.SocialSecurityNumber, v => v);
Author foundAuthor1 = authorDictionary[“555-55-5555”];
Julia Judith says
Thanks for this topic, its very useful
Optimist911 says
Multiple people have already expressed the same befuddlement. Same key, multiple values.
Ayush Vora says
Is the amortized lookup time constant?
Joe Frando says
Very simple: a lookup works like a Dictionary<KeyType, List>!