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 63071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:34:30+00:00 2026-05-10T18:34:30+00:00

After I read a bunch of LINQ related stuff, I suddenly realized that no

  • 0

After I read a bunch of LINQ related stuff, I suddenly realized that no articles introduce how to write asynchronous LINQ query.

Suppose we use LINQ to SQL, below statement is clear. However, if the SQL database responds slowly, then the thread using this block of code would be hindered.

var result = from item in Products where item.Price > 3 select item.Name; foreach (var name in result) {     Console.WriteLine(name); } 

Seems that current LINQ query spec doesn’t provide support to this.

Is there any way to do asynchronous programming LINQ? It works like there is a callback notification when results are ready to use without any blocking delay on I/O.

  • 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. 2026-05-10T18:34:31+00:00Added an answer on May 10, 2026 at 6:34 pm

    While LINQ doesn’t really have this per se, the framework itself does… You can easily roll your own asynchronous query executor in 30 lines or so… In fact, I just threw this together for you 🙂

    EDIT: Through writing this, I’ve discovered why they didn’t implement it. It cannot handle anonymous types since they are scoped local. Thus, you have no way of defining your callback function. This is a pretty major thing since a lot of linq to sql stuff creates them in the select clause. Any of the below suggestions suffer the same fate, so I still think this one is the easiest to use!

    EDIT: The only solution is to not use anonymous types. You can declare the callback as just taking IEnumerable (no type args), and use reflection to access the fields (ICK!!). Another way would be to declare the callback as ‘dynamic’… oh… wait… That’s not out yet. 🙂 This is another decent example of how dynamic could be used. Some may call it abuse.

    Throw this in your utilities library:

    public static class AsynchronousQueryExecutor {     public static void Call<T>(IEnumerable<T> query, Action<IEnumerable<T>> callback, Action<Exception> errorCallback)     {         Func<IEnumerable<T>, IEnumerable<T>> func =             new Func<IEnumerable<T>, IEnumerable<T>>(InnerEnumerate<T>);         IEnumerable<T> result = null;         IAsyncResult ar = func.BeginInvoke(                             query,                             new AsyncCallback(delegate(IAsyncResult arr)                             {                                 try                                 {                                     result = ((Func<IEnumerable<T>, IEnumerable<T>>)((AsyncResult)arr).AsyncDelegate).EndInvoke(arr);                                 }                                 catch (Exception ex)                                 {                                     if (errorCallback != null)                                     {                                         errorCallback(ex);                                     }                                     return;                                 }                                 //errors from inside here are the callbacks problem                                 //I think it would be confusing to report them                                 callback(result);                             }),                             null);     }     private static IEnumerable<T> InnerEnumerate<T>(IEnumerable<T> query)     {         foreach (var item in query) //the method hangs here while the query executes         {             yield return item;         }     } } 

    And you could use it like this:

    class Program {      public static void Main(string[] args)     {         //this could be your linq query         var qry = TestSlowLoadingEnumerable();          //We begin the call and give it our callback delegate         //and a delegate to an error handler         AsynchronousQueryExecutor.Call(qry, HandleResults, HandleError);          Console.WriteLine('Call began on seperate thread, execution continued');         Console.ReadLine();     }      public static void HandleResults(IEnumerable<int> results)     {         //the results are available in here         foreach (var item in results)         {             Console.WriteLine(item);         }     }      public static void HandleError(Exception ex)     {         Console.WriteLine('error');     }      //just a sample lazy loading enumerable     public static IEnumerable<int> TestSlowLoadingEnumerable()     {         Thread.Sleep(5000);         foreach (var i in new int[] { 1, 2, 3, 4, 5, 6 })         {             yield return i;         }     }  } 

    Going to go put this up on my blog now, pretty handy.

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

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Find the first row where there does not exist a… May 11, 2026 at 12:49 pm
  • added an answer private void CompressAndSaveImage(Image img, string fileName, long quality) { EncoderParameters… May 11, 2026 at 12:49 pm
  • added an answer Three things to make it work: regular expressions fuzzy logic… May 11, 2026 at 12:49 pm

Related Questions

I'm currently trying to read in an XML file, make some minor changes (alter
How long should it take to run ALTER DATABASE [MySite] SET READ_COMMITTED_SNAPSHOT ON I
After I was convinced that labeled breaks/continues are a total nono over here ,
After I messed up the description of my previous post on this I have
When parsing an xml document for its nodes or attributes, if the document is
I have an application that is database intensive. Most of the applications methods are
Alright so here is the deal. I am trying to make a application that's
I have two process and a shared memory zone, my workflow is like this.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.