Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7182431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:46:09+00:00 2026-05-28T17:46:09+00:00

MSDN says that ParallelEnumerable.GroupBy groups in parallel the elements of a sequence according to

  • 0

MSDN says that ParallelEnumerable.GroupBy groups in parallel the elements of a sequence according to a specified key selector function.
So my question is: How lazy it is ?

It’s clear that ParallelQuery<IGrouping<,>> is lazy. But what about IGrouping<> itself, is it lazy as well ?

So, if I do the following:

var entities = sites.AsParallel()
                         .Select(x => GetDataItemsFromWebsiteLazy(x))
                         .SelectMany(x => x)
                         .GroupBy(dataItem => dataItem.Url.Host)
                         .AsParallel()
                             .SelectMany(x => TransformToEntity(x));

Will TransformToEntity be called first time after all sites will fetch results?
Or as soon as first GetDataItemsFromWebsiteLazy() method will yield return an element?

The point of all that is to fire requests to different hosts in parallel.
Data processing goes as follows. For every website in a set:

  1. Request website
  2. Parse response and extract another site url
  3. Request site by extracted url
  4. Parse response and create entity from obtained data
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:46:10+00:00Added an answer on May 28, 2026 at 5:46 pm

    The GroupBy extension is, in fact, not lazy at all (or, more accurately, not deferred at all), as can be easily demonstrated with the following test program:

    void Main()
    {
        var source = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }.AsParallel();
        var groupEven = GetEvenNumbersUsingGroupBy(source);
        // foreach (int num in groupEven) { }
    }
    
    IEnumerable<int> GetEvenNumbersUsingGroupBy(IEnumerable<int> source)
    {
        Console.WriteLine("Method called: GetEvenNumbersUsingGroupBy");
        var grouped = source.GroupBy(i => i % 2);
        return grouped.Where(g => g.Key == 0).Single();
    }
    

    This program outputs the following:

    Method called: GetEvenNumbersUsingGroupBy

    Meaning that even though we never actually iterate the result of the GetEvenNumbersUsingGroupBy method, it still gets executed.

    This is in contrast to a normal deferred enumerable using the yield statement, as in:

    void Main()
    {
        var yieldEven = GetEvenNumbersUsingYield(source);
        foreach (int num in yieldEven) { }
        foreach (int num in yieldEven) { }
    }
    
    IEnumerable<int> GetEvenNumbersUsingYield(IEnumerable<int> source)
    {
        Console.WriteLine("Method called: GetEvenNumbersUsingYield");
        foreach (int i in source)
            if ((i % 2) == 0)   
                yield return i;
    }
    

    This prints the following:

    Method called: GetEvenNumbersUsingYield
    Method called: GetEvenNumbersUsingYield

    In other words, each time you iterate the results, they are re-evaluated, which is a typical characteristic of deferred evaluation (as opposed to straight-up lazy loading which caches the result after the first evaluation).

    Note that this is the same whether you use AsParallel or not; it’s a characteristic of the GroupBy extension (which by definition needs to build a hash table or other kind of lookup in order to store the individual groups) and wholly independent of concurrency.

    It’s easy to see why this is the case if you think about how you would implement a deferred grouping function; in order to iterate all of the elements of a single group, you would have to iterate the entire sequence to be sure that you’ve actually covered all of the elements of that group. So while it might technically be possible to defer this one-time iteration of the entire sequence, it’s probably not worth it in most cases, since it’s going to have the exact same memory and CPU characteristics as the eagerly-loaded version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The MSDN site says: From your view class's function that handles the WM_CREATE message
In MSDN it says that RegEnumValue should not be used when calling function that
MSDN says that the function SetDllDirectory() can be used to insert a directory into
MSDN says that GC calls the Win32 VirtualAlloc function to reserve segment of memory
MSDN says that you should use structs when you need lightweight objects. Are there
MSDN says that public static members of System.Windows.Application are thread safe. But when I
The MSDN says that using ReadDirectoryChangesW implies the calling process having the Backup and
What is the significance of the Thread.Join method in C#? MSDN says that it
The MSDN documentation says that public class SomeObject { public void SomeOperation() { lock(this)
http://msdn.microsoft.com/en-us/library/system.object.gethashcode(VS.80).aspx says: For the best performance, a hash function must generate a random distribution

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.