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

  • Home
  • SEARCH
  • 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 6365393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:17:14+00:00 2026-05-25T00:17:14+00:00

Taking my first stab as using the OnSelecting method of LinqDataSource so that I

  • 0

Taking my first stab as using the OnSelecting method of LinqDataSource so that I can specify a more complex query, I wrote this:

protected void CategoriesDataSource_OnSelecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        using (DataLayerDataContext db = new DataLayerDataContext())
        {
            e.Result = (from feed in db.Feeds
                        where feed.FeedName.StartsWith("Google")
                        select feed.MainCategory).Distinct();
        }
    }

The problem, of course, is that the using clause will Dispose the DataLayerDataContext. The ‘solution’ is to write it without it, but I’m afraid then that the context won’t be disposed of in a timely fashion, that it will leave a bunch of connections open until garbage collection runs, and so on.

I’m no expert in this area, so any comments on whether this is a real problem, or am I worried for naught?

  • 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-25T00:17:15+00:00Added an answer on May 25, 2026 at 12:17 am

    Ah, another person burned by the unfortunate side effects of deferred loading…

    protected void CategoriesDataSource_OnSelecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        using (DataLayerDataContext db = new DataLayerDataContext())
        {
            e.Result = (from feed in db.Feeds
                        where feed.FeedName.StartsWith("Google")
                        select feed.MainCategory).Distinct().ToList();
            //                                              ^^^^^^^^^
        }
    }
    

    This will force the DataLayerDataContext to immediately run the query and create an in-memory list that’s not dependent on the context or connection. That way you get your results immediately and you can dispose the context whenever you like.

    The only problem (per Steven’s comment) is the lazy-loaded navigation properties; even if you force the query to evaluate, if you try to query any properties that are references to other LINQ objects (or lists of LINQ objects) they will fail to load unless you specify what properties are immediately required in the DataLoadOptions of the DataContext. See the below for an example:

    protected void CategoriesDataSource_OnSelecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        using (DataLayerDataContext db = new DataLayerDataContext())
        {
            DataLoadOptions loadOptions = new DataLoadOptions();
            loadOptions.LoadWith<Category>(c => c.SomeReference);
            loadOptions.LoadWith<Category>(c => c.SomeOtherReferences);
    
            db.LoadOptions = loadOptions;
    
            e.Result = (from feed in db.Feeds
                        where feed.FeedName.StartsWith("Google")
                        select feed.MainCategory).Distinct().ToList();
        }
    }
    

    Once you manually specify these associations, LINQ to SQL will eagerly load them into memory when the query executes, instead of leaving them to deferred loading (which will cause an exception when the properties are used after the DataContext is disposed.)

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

Sidebar

Related Questions

I am taking my first steps programming in Lua and get this error when
Taking over some code from my predecessor and I found a query that uses
I'm taking a stab at writing a bash completion for the first time, and
I am taking my first steps in XML-schema land, using XSD, and I'm trying
I am taking my first teetering steps with submitting Html forms using jQuery. All
I am taking my first foray into PHP programming and need to configure the
Taking advice from this post , I purchased a copy of 'The C Programming
Taking this article on classes and structs as an example: http://msdn.microsoft.com/en-us/library/ms173109.aspx namespace ProgrammingGuide {
I'm taking the leap: my PHP scripts will ALL fail gracefully! At least, that's
Taking my first course in assembly language, I am frustrated with cryptic error messages

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.