dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersGaurav Arora
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » LINQ Samples

LINQ: ToLookup() vs ToDictionary()


Posted Date:     Category: LINQ Samples    
Author: Member Level: Gold    Points: 7


This is about a new feature in .net called ToLookup() a extension method on IEnumerable objects. Most of us have used Dictionary object which will have a key value pair. But what if you want to have multiple value for a single key? There you need this lookpu, you have use this ToLookup() extension method and make a lookup object which will have key, collection of values on it.



 


Lookup is a .net type which is a key value pair, where one key can point to multiple values. Dictionary is one-to-one so if you try to add the same key it will throw exception. Where as lookup is one-to-many so same key is allowed.



var products = new List

{

new Product { Id = 1, Category = "Electronics", Value = 15.0 },

new Product { Id = 1, Category = "Groceries", Value = 40.0 },

new Product { Id = 1, Category = "Garden", Value = 210.3 },

new Product { Id = 4, Category = "Pets", Value = 2.1 },

new Product { Id = 5, Category = "Electronics", Value = 19.95 },

new Product { Id = 6, Category = "Pets", Value = 21.25 },

new Product { Id = 7, Category = "Pets", Value = 5.50 },

new Product { Id = 8, Category = "Garden", Value = 13.0 },

new Product { Id = 9, Category = "Automotive", Value = 10.0 },

new Product { Id = 10, Category = "Electronics", Value = 250.0 },

};

//one to many, immutable

var lookup = products.ToLookup(p => p.Id, p => p);

var productIdsByCategory = products.ToLookup(p => p.Category, p => p.Id);

foreach(var lookupGroup in lookup[1])

{

Response.Write(lookupGroup.ToString()+"
");

}

//one to one, exception will be thrown, mutable

//var dictionary = products.ToDictionary(p => p.Id, p => p);

var group = products.GroupBy(g => g.Category);

var dictionary1 = group.ToDictionary(p => p.Key, p => p.ToList());

dictionary1.Add("NewCategory", new List

{

new Product { Id = 2, Category = "NewCategory", Value = 15.0 },

new Product { Id = 2, Category = "NewCategory", Value = 40.0 },

});








Did you like this resource? Share it with your friends and show your love!


Responses to "LINQ: ToLookup() vs ToDictionary()"
Author: Siva Prasad    22 Jul 2012Member Level: Gold   Points : 0
While executing this I'm facing compile time errors.


Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Update the Attribute value using LINQ
    Previous Resource: Iterate two lists using linq, how to convert a for loop to linq method group
    Return to Resources
    Post New Resource
    Category: LINQ Samples


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    ToLookup()  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.