By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

[LINQ] GroupBy vs ToLookup

P: n/a
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

Mar 3 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Mar 3 '08 #2

P: n/a
>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.

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.
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

Mar 3 '08 #3

P: n/a
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
Mar 3 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.