Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

[LINQ] GroupBy vs ToLookup

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Wiktor Zychla [C# MVP] - 03 Mar 2008 13:39 GMT
could someone enlighten me on what would be the difference between GroupBy
and ToLookup?

I try hard but am not able to spot any difference between these two. the
syntax and behavioral semantics is the same.

is there any explanation on why we need them both?

Thanks in advance,
Wiktor Zychla
Marc Gravell - 03 Mar 2008 14:23 GMT
Well, ToLookup is buffered - i.e. it is expected that you can ask (by key)
repeatedly. GroupBy, on the other hand, simply iterates the groups
(streaming).

Under the bonnet, I suspect that LINQ-to-object uses ToLookup to perform
GroupBy, but the difference is greater for other providers, such as a
database - indeed, for streaming providers (such as TDS), where both are
supported it is likely that the roles are reversed, and ToLookup consumes
GroupBy.

Marc
Jon Skeet [C# MVP] - 03 Mar 2008 15:16 GMT
> Well, ToLookup is buffered - i.e. it is expected that you can ask (by key)
> repeatedly. GroupBy, on the other hand, simply iterates the groups
[quoted text clipped - 5 lines]
> supported it is likely that the roles are reversed, and ToLookup consumes
> GroupBy.

In LINQ to Objects, GroupBy is definitely buffered - and has to be,
because it can't give results to a downstream consumer until it's seen
all the upstream results.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Wiktor Zychla [C# MVP] - 03 Mar 2008 15:47 GMT
>> Under the bonnet, I suspect that LINQ-to-object uses ToLookup to perform
>> GroupBy, but the difference is greater for other providers, such as a
[quoted text clipped - 5 lines]
> because it can't give results to a downstream consumer until it's seen
> all the upstream results.

nice, as it still does not reveil my confussion.

could you be so nice and provide an example where using GroupBy vs ToLookup
would lead to different results?

Wiktor Zychla
Jon Skeet [C# MVP] - 03 Mar 2008 16:05 GMT
> >> Under the bonnet, I suspect that LINQ-to-object uses ToLookup to perform
> >> GroupBy, but the difference is greater for other providers, such as a
[quoted text clipped - 10 lines]
> could you be so nice and provide an example where using GroupBy vs ToLookup
> would lead to different results?

For one thing (which I forgot to mention before), GroupBy uses deferred
execution whereas ToLookup uses immediate execution.

In other words, suppose you call GroupBy, then Console.WriteLine, then
foreach over the results - the Console.WriteLine will happen before the
grouping is actually executed.

If you call ToLookup, then Console.WriteLine, then foreach over the
results - the Console.WriteLine happens *after* all the grouping is
executed.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

wzychla@gmail.com - 03 Mar 2008 18:57 GMT
> For one thing (which I forgot to mention before), GroupBy uses deferred
> execution whereas ToLookup uses immediate execution.

that should be it - the docs says that conversion operators (ToList,
ToArray, ToLookup, ToDictionary) causes queries to be executed
immediately, where GroupBy can be, of course, deferred.

thanks, Jon.
Wiktor Zychla

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.