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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:16:49+00:00 2026-05-11T02:16:49+00:00

Imagine I have the following: private IEnumerable MyFunc(parameter a) { using(MyDataContext dc = new

  • 0

Imagine I have the following:

private IEnumerable MyFunc(parameter a) {    using(MyDataContext dc = new MyDataContext)    {       return dc.tablename.Select(row => row.parameter == a);    } }  private void UsingFunc() {    var result = MyFunc(new a());     foreach(var row in result)    {       //Do something    } } 

According to the documentation the linq execution will defer till I actual enumerate the result, which occurs in the line at the foreach. However the using statement should force the object to be collected reliably at the end of the call to MyFunct().

What actually happens, when will the disposer run and/or the result run?

Only thing I can think of is the deferred execution is computed at compile time, so the actual call is moved by the compiler to the first line of the foreach, causing the using to perform correctly, but not run until the foreach line? Is there a guru out there who can help?

EDIT: NOTE: This code does work, I just don’t understand how.

I did some reading and I realised in my code that I had called the ToList() extension method which of course enumerates the result. The ticked answer’s behaviour is perfectly correct for the actual question answered.

Sorry for any confusion.

  • 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-11T02:16:49+00:00Added an answer on May 11, 2026 at 2:16 am

    I would expect that to simply not work; the Select is deferred, so no data has been consumed at this point. However, since you have disposed the data-context (before leaving MyFunc), it will never be able to get data. A better option is to pass the data-context into the method, so that the consumer can choose the lifetime. Also, I would recommend returning IQueryable<T> so that the consumer can ‘compose’ the result (i.e. add OrderBy / Skip / Take / Where etc, and have it impact the final query):

    // this could also be an instance method on the data-context internal static IQueryable<SomeType> MyFunc(     this MyDataContext dc, parameter a) {    return dc.tablename.Where(row => row.parameter == a); }  private void UsingFunc() {     using(MyDataContext dc = new MyDataContext()) {        var result = dc.MyFunc(new a());         foreach(var row in result)        {            //Do something        }     } } 

    Update: if you (comments) don’t want to defer execution (i.e. you don’t want the caller dealing with the data-context), then you need to evaluate the results. You can do this by calling .ToList() or .ToArray() on the result to buffer the values.

    private IEnumerable<SomeType> MyFunc(parameter a) {    using(MyDataContext dc = new MyDataContext)    {       // or ToList() etc       return dc.tablename.Where(row => row.parameter == a).ToArray();    } } 

    If you want to keep it deferred in this case, then you need to use an ‘iterator block’:

    private IEnumerable<SomeType> MyFunc(parameter a) {    using(MyDataContext dc = new MyDataContext)    {       foreach(SomeType row in dc           .tablename.Where(row => row.parameter == a))       {         yield return row;       }    } } 

    This is now deferred without passing the data-context around.

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • 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
  • Editorial Team
    Editorial Team added an answer VerifyGet is enough, assert seems to add no value so… May 12, 2026 at 1:03 am
  • Editorial Team
    Editorial Team added an answer Your code looks right (I haven't tested it), and the… May 12, 2026 at 1:03 am
  • Editorial Team
    Editorial Team added an answer Command-line scripting can be very useful, even in the Windows… May 12, 2026 at 1:03 am

Related Questions

Here's the situation: I'm developing a simple application with the following structure: FormMain (startup
Lots of developers think that testing private methods is a bad idea. However, all
Let's pretend I have the following xaml... <UserControl.Resources> <local:ViewModel x:Name=viewModel /> <local:LoadChildrenValueConverter x:Name=valueConverter />
I just saw this question: Understanding .NET’s SecurityAction parameter for permissions And I have

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.