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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:17:02+00:00 2026-05-18T01:17:02+00:00

I use LINQ 2 SQL in one of my projects and i have numerous

  • 0

I use LINQ 2 SQL in one of my projects and i have numerous relationships Customer -> Documents1, Documents2, Documents3, Address, Invoices etc….

When using the LoadWith(p => p.Documents1)…etc
I have performance issues, imagine 2000 customers with all these numerous relationships loaded in List in memory!

The other way around Document -> Customer its not so much an issue as the relationship its light.

So i try to remove all the LoadWith and just leave the Customer -> Address relationship.
Now if i go and open a Document1 and then open my Customers i get an object disposed exception when i serialize my Customers. The serialize method basically throws this exception.

The serialize Method:

public static T CloneObjectGraph<T>(this T obj) where T : class 
        {
            var serializer = new DataContractSerializer(typeof(T), null, int.MaxValue, false, true, null);
            using (var ms = new System.IO.MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                ms.Position = 0;
                return (T)serializer.ReadObject(ms);
            }
        }

The Method i get the Customers:

public List<Customer> GetCustomers()
{
    using (MyDataContext db = new MyDataContext(MyDataContextManager.ConnectionString))
    {
        DataLoadOptions dlo = new DataLoadOptions();
        dlo.LoadWith<Customer>(p => p.Address);
        dlo.LoadWith<Customer>(p => p.Documents1);
        dlo.LoadWith<Customer>(p => p.Documents2);
        dlo.LoadWith<Customer>(p => p.Documents3);
        dlo.LoadWith<Customer>(p => p.Documents4);
        dlo.LoadWith<Customer>(p => p.Documents5);
        dlo.LoadWith<Customer>(p => p.Documents6);
        dlo.LoadWith<Customer>(p => p.Documents7);
        dlo.LoadWith<Customer>(p => p.Documents8);
        dlo.LoadWith<Customer>(p => p.Documents9);
        dlo.LoadWith<Customer>(p => p.Documents10);
        dlo.LoadWith<Customer>(p => p.Documents11);

        db.LoadOptions = dlo;

        return db.Customers.ToList();
    }
}

I want to remove all the LoadWith except Address relationship.
I hate when this error is not reproduce always but in some cases i couldn’t find.

I could guess the DataContractSerializer constructor for a change but i cant get it right.

Any help appreciated!

  • 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-18T01:17:03+00:00Added an answer on May 18, 2026 at 1:17 am

    Your error is occurring because your clone method is going to attempt to access all of the child properties of your object. When you have the LoadWith<>() statements, those child properties are already retrieved from the database and available in memory. When you remove the LoadWith<>() statements, the properties will attempt to Lazy Load the objects from the database. Because you’ve already closed the database connection, those properties cannot be loaded (and you get the error).

    The solution depends on what you are attempting to accomplish by performing a deep copy of the objects; If that deep copy actually does need to have the child properties (DocumentsXX), then you either have to leave the LoadWith<>() statements or you need the database connection to stay open during the process (of the two, leaving the LoadWith<>() statements is probably the better option because it minimizes the accesses to the DB and ends up using the same amount of memory anyway). If the deep copy really doesn’t need to include those properties, then you need to replace the current deep clone process with on that can ignore those properties (either a manually created deep clone or a customized serialization process would work).

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

Sidebar

Related Questions

In my previous applications when I used linq-to-sql I would always use one class
I use LINQ-SQL as my DAL, I then have a project called DB which
Trying to use Linq to SQL for a small project I'm working on at
I'm trying to use LINQ to SQL to select a few specific columns from
I'd like to use Linq to SQL in my windows form application. The data
I'm trying to use linq to sql for my project (very short deadline), and
I'm trying to use LINQ to SQL Server in C#/.NET on Visual Studio 2010.
I am wondering if you can use Linq to SQL using a .mdf file
Since I'm using Postgresql and can't use LINQ to SQL, I wrote my own
My app in ASP.NET C#, i use linq to sql to pass data from

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.