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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:18:01+00:00 2026-05-15T00:18:01+00:00

Update – The answer is apparently that DbLinq doesn’t implement Dispose() properly. D’oh! The

  • 0

Update – The answer is apparently that DbLinq doesn’t implement Dispose() properly. D’oh!


The below is all sort of misleading – Bottom line: DbLinq is not (yet) equivalent to LinqToSql, as I assumed when I originally asked this question. Use it with caution!

I’m using the Repository Pattern with DbLinq. My repository objects implement IDisposable, and the Dispose() method does only thing–calls Dispose() on the DataContext. Whenever I use a repository, I wrap it in a using block, like this:

public IEnumerable<Person> SelectPersons()
{
    using (var repository = _repositorySource.GetPersonRepository())
    {
        return repository.GetAll(); // returns DataContext.Person as an IQueryable<Person>
    }
}

This method returns an IEnumerable<Person>, so if my understanding is correct, no querying of the database actually takes place until Enumerable<Person> is traversed (e.g., by converting it to a list or array or by using it in a foreach loop), as in this example:

var persons = gateway.SelectPersons();
// Dispose() is fired here
var personViewModels = (
    from b in persons
    select new PersonViewModel
    {
        Id = b.Id,
        Name = b.Name,
        Age = b.Age,
        OrdersCount = b.Order.Count()
    }).ToList(); // executes queries

In this example, Dispose() gets called immediately after setting persons, which is an IEnumerable<Person>, and that’s the only time it gets called.

So, three questions:

  1. How does this work? How can a disposed DataContext still query the database for results after the DataContext has been disposed?
  2. What does Dispose() actually do?
  3. I’ve heard that it is not necessary (e.g., see this question) to dispose of a DataContext, but my impression was that it’s not a bad idea. Is there any reason not to dispose of a DbLinq DataContext?
  • 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-15T00:18:02+00:00Added an answer on May 15, 2026 at 12:18 am

    1 How does this work? How can a disposed DataContext still query the database for results after the DataContext has been disposed?

    It doesn’t work. There’s something you’re not showing us. I’m guessing that either your repository class doesn’t dispose the DataContext properly/at the right time, or that you are perfunctorily writing ToList() at the end of every query, which completely negates the query transformation and deferred execution you normally get.

    Try the following code in a test app, I guarantee you that it will throw an ObjectDisposedException:

    // Bad code; do not use, will throw exception.
    IEnumerable<Person> people;
    using (var context = new TestDataContext())
    {
        people = context.Person;
    }
    foreach (Person p in people)
    {
        Console.WriteLine(p.ID);
    }
    

    This is the simplest possible reproducible case, and it will always throw. On the other hand, if you write people = context.Person.ToList() instead, then the query results have already been enumerated inside the using block, which I’ll bet is what’s happening in your case.

    2 What does Dispose() actually do?

    Among other things, it sets a flag indicating that the DataContext is disposed, which is checked on every subsequent query and causes the DataContext to throw an ObjectDisposedException with the message Object name: 'DataContext accessed after Dispose.'.

    It also closes the connection, if the DataContext opened it and left it open.

    3 I’ve heard that it is not necessary (e.g., see this question) to dispose of a DataContext, but my impression was that it’s not a bad idea. Is there any reason not to dispose of a LinqToSql DataContext?

    It is necessary to Dispose the DataContext, as it is necessary to Dispose every other IDisposable. You could potentially leak connections if you fail to dispose the DataContext. You could also leak memory if any of the entities retrieved from the DataContext are kept alive, since the context maintains an internal identity cache for the unit-of-work pattern it implements. But even if none of this were the case, it is not your concern what the Dispose method does internally. Assume that it does something important.

    IDisposable is a contract that says, “cleanup may not be automatic; you need to dispose me when you’re finished.” You have no guarantees of whether or not the object has its own finalizer that cleans up after you if you forget to Dispose. Implementations are subject to change, which is why it’s not a good idea to rely on observed behaviour as opposed to explicit specifications.

    The worst thing that can happen if you dispose an IDisposable with an empty Dispose method is that you waste a few CPU cycles. The worst thing that can happen if you fail to dispose an IDisposable with a non-trivial implementation it is that you leak resources. The choice here is obvious; if you see an IDisposable, don’t forget to dispose it.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer >>> l = [{'id':1,'name':'Foo'},{'id':2,'name':'Bar'}] >>> [tuple(d.values()) for d in l]… May 15, 2026 at 4:58 pm
  • Editorial Team
    Editorial Team added an answer Assuming you have PHP's CLI installed, just do php -a… May 15, 2026 at 4:58 pm
  • Editorial Team
    Editorial Team added an answer Try and find out exactly what's happening when you log-in… May 15, 2026 at 4:58 pm

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.