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

  • Home
  • SEARCH
  • 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 616257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:18:27+00:00 2026-05-13T18:18:27+00:00

i have an extension method for IEnumerable which then iterates through the collection, doing

  • 0

i have an extension method for IEnumerable which then iterates through the collection, doing its business, and then returns an new IEnumerable.

I have tried to use PLINQ using .AsParallel().ForAll() which speeds up the iterations significantly (which of course it should do) however when the collection is returned, there are often a few objects in that collection that are null.

I’m assuming this may be because it is returning the collection before all the ‘business’ has a chance to complete? if i debug and put in a breakpoint, there are no nulls.

is there some sort of ‘wait for this operation to be completed’ method that i should be using?

EDIT: to be a bit clearer, there is business logic in the forall, modifying properties etc. it’s necessary to have an action looped, rather than simply selecting something.

  • 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-13T18:18:28+00:00Added an answer on May 13, 2026 at 6:18 pm

    The answer depends on what you mean by returning, because the ForAll method doesn’t return anything. It invokes the delegate you specify in parallel for all elements of the collection. I would suppose that your code looks like this:

    data.AsParallel().ForAll(() => /* calculate and store result somewhere */);
    // more code
    

    The ForAll method doesn’t wait for all delegates to complete, so more code can execute before all the delegates complete (and you also need to be careful in the store result somewhere bit, because it may run concurrently for multiple delegates!)

    I think the code could be rewritten more elegantly using the Select method:

    var res = data.AsParallel().Select(() => /* calculate the result */);
    

    In this case, the delegate simply returns the result. The Where method collects all the results and when you iterate over the returned IEnumerable, it guarantees that all delegates finished calculating.

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

Sidebar

Related Questions

I have an extension method called ToListIfNotNullOrEmpty() , which is hitting the DB twice,
I have the following extension method public static class ListExtensions { public static IEnumerable<T>
I have an extension method on the HttpApplicationState object for getting my IoC container
I have a simple extension method on the int type so I can do
Does Python have extension methods like C#? Is it possible to call a method
I would like to create a safe sum extension method that would have the
I have the below code, which iterates over a list based on a custom
I have an extension method with the following signature (in BuildServerExtensions class):: public static
I have an interesting need for an extension method on the IEumerable interface -
I am trying to do use .Select extension method on ListView.SelectedItems which is SelectedListViewItemCollection

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.