The title is the exception that is thrown after I access joined objects after generating the data with the following method using LINQ2SQL:
public static bool Get(int skip, int take, ListSortDirection direction, out List<Post> posts)
{
bool result;
using (var db = new MyDataContext())
{
try
{
posts = db.Posts
.Where(p => !p.IsDeleted)
.OrderBy(p => p.DateTimeCreated, direction)
.Skip(skip)
.Take(take)
.ToList();
result = true;
}
catch (Exception)
{
// Log the exception
posts = new List<Post>();
result = false;
}
}
return result;
}
Below is an example of the join object that I'm referring to. In the method above, I'm selecting a collection of posts and the userprofileID of the Userprofile table is joined (in the .dbml and the database server) to the UserprofileID of the Post table.
The problem is that when I examine the collection of posts generated, the linked Userprofile returns the following exception:
new System.Collections.Generic.Mscorlib_CollectionDebugView(posts).Items[0].Userprofile' threw an exception of type 'System.ObjectDisposedException'
I've tried disabling the DeferredLoadingEnabled attribute in the datacontext and using DataLoadOptions:
var dlo = new DataLoadOptions();
dlo.LoadWith<Post>(p => p.Userprofile);
db.LoadOptions = dlo;
Which resulted in the same exception;
Is there anything else I should be looking at?
Thanks!
catch (Exception)? The wholetry/catchlike that is a bad anti-pattern. Have a read of Eric Lippert's Vexing Exceptions. - Enigmativity