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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:10:10+00:00 2026-05-27T08:10:10+00:00

I know this is a crazy thing to ask.. but Ill give it a

  • 0

I know this is a crazy thing to ask.. but Ill give it a shot 🙂
Is there anyway to build up your linq/query and then later on be able to build even more on it and then execute it.. for instance like this:

        public void Test()
    {
        var k = TestRepo();
        k = k.Where(e => e.SomeColumn == someValue);

        rp.DataSource = k.Select(t => t.Id);
        rp.DataBind();
    }


    public IEnumerable<ApplicationEntity> TestRepo()
    {
        using (var x = new MyEntityContext())
        {
            return from q in x.MyColumn
                   select q;
        }
    }

If im not misstaken this whould never work :)… but whouldnt it be just great if you first could “build” the basics of the query and then extend it somemore and then once its finaly ready actually execute it and get the result..
So is there anyway to do this as in the example above with EF or NHibernate.. or both of em?

Thanks in advance!

  • 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-27T08:10:11+00:00Added an answer on May 27, 2026 at 8:10 am

    The trick is not to dispose the MyEntityContext. For instance, you can cache it for the lifetime of a request and dispose it at the end of an request. This might look like this:

    public static class ContextFactory
    {
    private static bool disposeRegistered = false;
    
    public static MyEntityContext GetContext()
    {
        RegisterDispose();
    
        var instance = (MyEntityContext)HttpContext.Current
            .Items["MyEntityContext"];
    
        if (instance == null)
        {
            instance = new MyEntityContext();
    
            HttpContext.Current.Items["MyEntityContext"] =
                instance;
        }
    
        return instance;    
    }
    
    private static void RegisterDispose()
    {
        if (disposeRegistered)
            return;
    
        HttpContext.Current.ApplicationInstance
           .EndRequest += (s,e) =>
        {
            var instance = HttpContext.Current
                .Items["MyEntityContext"] as IDisposable;
    
            if (instance != null)
            {
                instance.Dispose();
            }
        };
    
        this.disposeRegistered = true;
    }
    }
    

    The TestRepo method than looks like this:

    public IQueryable<Employee> TestRepo()
    {
        var x = ContextFactory.GetContext()
    
        return 
            from employee in x.Employees
            where employee.Salary > 1000
            select employee;
    }
    

    Note how the TestRepo now returns IQueryable. This allows other methods to change the query and only when you start iterating it, the query will get sent to the database.

    NOTE: Instead of taking a dependency on a static ContextFactory class, you could also inject an MyEntityContext into types that wish to use it. You can use this by using a technique called Dependency Injection.

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

Sidebar

Related Questions

Maybe I don't know how to ask/search for this particular thing, but basically I
This is a bit of a crazy question, but does anyone out there know
I'm always struggling with generics. I don't know why this makes my go crazy.
Ok, this is driving me crazy. I have a CA that needs to know
Know this might be rather basic, but I been trying to figure out how
(I know this sounds like a duplicate, but hear me out) Given the following
I know this has been asked in other places and answered, but I'm having
I know this has been discussed time and time again, but I can't seem
I know this isn't the most ideal solution, but I need to add an
I know this is probably really dumb, but I can't figure out why this

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.