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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:09:48+00:00 2026-05-17T19:09:48+00:00

Interesting problem I ran across which makes total sense. I have a generic method

  • 0

Interesting problem I ran across which makes total sense. I have a generic method like so:

public TResult Run<TResult>(Func<SqlDataReader, TResult> resultDelegate)
{
   TResult result;

   using (SqlDataReader reader = command.ExecuteReader()) // command is SqlCommand with attached SqlConnection
   {
      result = resultsDelegate(reader);
   }

   // Some other unrelated code (but that's why result has a variable)

   return result;
}

In one case, the resultDelegate‘s return type (TResult) is IEnumerable<object>. The problem is that the Run function returns immediately due to deferred execution, disposing the SqlDataReader. Later in the code when I try to read through the results (which the delegate does reader.Read(), I get an InvalidOperationException: Invalid attempt to call Read when reader is closed.

I’m having a hard time figuring out the best way around this. I know I can return a concrete list, but I would like to avoid that if possible. I can also move the using statement inside the delegate, but once again, if I can avoid doing that for every delegate it would be nice. Any ideas?

  • 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-17T19:09:48+00:00Added an answer on May 17, 2026 at 7:09 pm

    Perhaps:

    public TResult Run<TResult>(Func<SqlDataReader, TResult> resultDelegate)
    {
       TResult result;
    
       using (SqlDataReader reader = command.ExecuteReader()) // command is SqlCommand with attached SqlConnection
       {
          result = resultsDelegate(reader);
          if (typeof(TResult) == typeof(IEnumerable<object>)) 
          {
             var enumerable = result as IEnumerable<object>;
             if (enumerable != null) 
             {
                result = enumerable.ToList();  
             }
          }
       }
    
       // Some other unrelated code (but that's why result has a variable)
    
       return result;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run across an interesting problem. I am sending email with attachments through
So I came across an interesting problem today. We have a WCF web service
I have a very interesting problem on my LinqToSql model. On some of my
He're an interesting problem that looks for the most Pythonic solution. Suppose I have
I have an interesting problem and would appreciate your thoughts for the best solution.
I have one interesting problem. I must parse mail body (regular expression), get some
I came across an interesting problem today. I was altering a stored procedure and
I have an interesting problem and I was looking for a solution; Hoping that
Here's an interesting problem. I have an ETL script written in c# that I
Here is a an interesting problem. I have a view controller, let's call it

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.