This question is purely academic, because I'd never dream of doing this in real code.
Using LINQ to SQL, I want to bind an IQueryable<T>
to a GridView. I tried doing this with the following code, but I get the exception:
Cannot access a disposed object. Object name: 'DataContext accessed after Dispose.'.
Here is my code that gets an IQueryable<tabGenre>
using LINQ to SQL:
public static IQueryable<lu_Genre> GetGenres2() {
using (BooksContextDataContext ctx = new BooksContextDataContext()) {
IQueryable<tabGenre> iq = from g in ctx.tabGenre
select g;
return iq;
}
}
And here is my code that binds the GridView to the returned IQueryable<T>
.
private void GetGenres() {
gvGenre.DataSource = Genre.GetGenres2();
gvGenre.DataBind();
}
So why doesn't this work? I could just a .ToList<tabGenre>()
, return it, bind to it and then it would work, but why doesn't IQueryable work in the same fashion? What I'm really trying to achieve here is an understanding of what IQueryable can do.
EDIT: I also tried disabling lazy loading, but to no affect.