[LINQ] GroupBy vs ToLookup

  • Thread starter Thread starter Wiktor Zychla [C# MVP]
  • Start date Start date
W

Wiktor Zychla [C# MVP]

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
 
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
 
Marc Gravell said:
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.

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.
 
Under the bonnet, I suspect that LINQ-to-object uses ToLookup to perform
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
 
Wiktor Zychla said:
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?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top