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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:12:53+00:00 2026-05-23T22:12:53+00:00

I have created a simple repository class that is returning a list of Projects:

  • 0

I have created a simple repository class that is returning a list of Projects:

public class ProjectRepository : IProjectRepository
{
    public IQueryable<Project> GetProjects() 
    {
        DbEntities db = new DbEntities();

        return db.Projects;
    }
}

I have some code later down the line that is working off this IQueryable to do some sorting/paging:

public ActionResult Index()
{
    var projectList = (from p in projectRepository.GetProjects()
                      orderby p.Name
                      select p).Take(20);

    return View(projectList.ToList());
}

I originally wrapped my GetProjects() call in a using statement out of habit – since it’s usually a good idea to dispose your objects as soon as possible. This caused problems understandably because I was doing some further database work in my controller action.

Is it okay to just let the object be disposed when it is garbage collector? Is there a better way to do this instead? Should I be creating more targeted repository methods like:

public IQueryable<Project> GetProjects(int takeThisMany)
{
    return (from p in db.Projects
           orderby p.Name
           select p).Take(takeThisMany);
}

Is it a good or bad practice to return IQueryable objects that will be executed at a later time in a controller action without disposing the original context or not?

  • 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-23T22:12:54+00:00Added an answer on May 23, 2026 at 10:12 pm

    It’s fine to return IQueryable<T>, but wrong to either not dispose an ObjectContext (you should always dispose any IDisposable) or to dispose it before the request is complete.

    Use constructor injection to pass the context to the repository and dispose it at the end of the request.

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

Sidebar

Related Questions

i have created a simple public ref class in the vc++ project, which is
I have created a repository that is returning data from my database using Entity
I have a repository like that: public class Repository<T> : IRepository<T> where T :
I am new to Repository concept and get some questions. I have created simple
I have a class that outputs a simple report file. It reads some record
A. Simple question. I have 3 instances of Repository that depends on the Configuration.
I currently have an ASP.NET MVC 3 project, plus a class library project, that
Could someone help me on this, I have created simple web services using axis2
How to manually create Friendly URLs? (PHP) So I have created simple php file
I have created a simple wcf service which used the WCF Service Library template.

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.