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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:02:20+00:00 2026-05-15T02:02:20+00:00

I have a member class that returned IQueryable from a data context public static

  • 0

I have a member class that returned IQueryable from a data context

    public static IQueryable<TB_Country> GetCountriesQ()
    {
        IQueryable<TB_Country> country;

        Bn_Master_DataDataContext db = new Bn_Master_DataDataContext();

        country = db.TB_Countries
            .OrderBy(o => o.CountryName);

        return country;
    }

As you can see I don’t delete the data context after usage. Because if I delete it, the code that call this method cannot use the IQueryable (perhaps because of deferred execution?). How to force immediate execution to this method? So I can dispose the data context..

Thank you 😀

  • 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-15T02:02:21+00:00Added an answer on May 15, 2026 at 2:02 am

    The example given by Codeka is correct, and I would advice writing your code with this when the method is called by the presentation layer. However, disposing DataContext classes is a bit tricky, so I like to add something about this.

    The domain objects generated by LINQ to SQL (in your case the TB_Countries class) often contain a reference to the DataContext class. This internal reference is needed for lazy loading. When you access for instance list of referenced objects (say for instance: TB_Country.States) LINQ to SQL will query the database for you. This will also happen with lazy loaded columns.

    When you dispose the DataContext, you prevent it from being used again. Therefore, when you return a set of objects as you’ve done in your example, it is impossible to call the States property on a TB_Country instance, because it will throw a ObjectDisposedException.

    This does not mean that you shouldn’t dispose the DataContext, because I believe you should. How you should solve this depends a bit on the architecture you choose, but IMO you basically got two options:

    Option 1. Supply a DataContext to the GetCountriesQ method.
    You normally want to do this when your method is an internal method in your business layer and it is part of a bigger (business) transaction. When you supply a DataContext from the outside, it is created outside of the scope of the method and it shouldn’t dispose it. You can dispose it at a higher layer. In that situation your method basically looks like this:

    public static IQueryable<TB_Country> GetCountriesQ(
        Bn_Master_DataDataContext db)
    {
        return db.TB_Countries.OrderBy(o => o.CountryName);
    }
    

    Option 2. Don’t return any domain objects from the GetCountriesQ method.
    This solution is useful when the method is a public in your business layer and will be called by the presentation layer. You can wrap the data in a specially crafted object (a DTO) that contains only data and no hidden references to the DataContext. This way you have full control over the communication with the database and you can dispose the DataContext as you should. I’ve written more about his on SO here. In that situation your method basically looks like this:

    public static CountryDTO[] GetCountriesQ()
    {
        using (var db = new Bn_Master_DataDataContext())
        {
            var countries;
                from country in db.TB_Countries
                orderby country.CountryName
                select new CountryDTO()
                {
                    Name = country.CountryName,
                    States = (
                        from state in country.States
                        order by state.Name
                        select state.Name).ToList();
                };
    
            return countries.ToArray();
        }
    }
    
    public class CountryDTO
    {
        public string Name { get; set; }
        public List<StateDTO> States { get; set; }
    }
    

    As you will read here there are some smart things you can do that make using DTOs less painful.

    I hope this helps.

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

Sidebar

Ask A Question

Stats

  • Questions 454k
  • Answers 454k
  • 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 Tricky. You could select nodes that have preceding siblings: A/B[preceding-sibling::*]… May 15, 2026 at 9:57 pm
  • Editorial Team
    Editorial Team added an answer chardet.detect() returns a dictionary which provides the encoding as the… May 15, 2026 at 9:57 pm
  • Editorial Team
    Editorial Team added an answer You can use strtotime() like this : strtotime("-4 seconds", $reference_time);… May 15, 2026 at 9:57 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.