nakama

Hi, I think this would be FAQ, please tell me the pointer.

How can I return the entity collections beyond the DataContext
LINQ to SQL evaluate the query on-demand and use the deferred loading, but this behavior cause the difficulty of returning the values beyond the DataContext.

Code Snippet

class Program
{
static void Main(string[] args)
{
var authors = ReturnSample1();
foreach (var a in authors) Console.WriteLine(a); // ObjectDisposedException occurred !!
}

static IQueryable ReturnSample1()
{
using (PubsDataContext pubsDB = new PubsDataContext())
{
pubsDB.DeferredLoadingEnabled = false;
var authors = from a in pubsDB.authors
select new { a.au_id, au_name = a.au_fname + " " + a.au_lname };
return authors;
}
}
}

How can I avoid this exception (i.e., how to force the data loading in LINQ to SQL )

Any suggestions are appreciated.

Thanks.



Re: LINQ Project General How to return the entity objects beyond the DataContext?

Anders Borum

Hi!

You probably want to return an object of type IEnumerable (i.e. the List type). You should consider the DataContext as a means to accessing your data and subsequently determine, what type to return from the queries against the DataContest.

The LINQ infrastructure provides the necessary implementation to return a query (i.e. your query for authors) as a list of T (i.e. List). In your case, the resulting entity type from the query is one with two properties (au_id and au_name). If you want to return the result from the method, you should implement a concrete type that provides the interface for the caller.

Code Snippet

public class Author
{
public int au_id;
public string au_name;
}

private List<Author> GetAuthors()
{
List<Author> result = null;

using (PubsDataContext pubsDB = new PubsDataContext())
{
result = (from a in pubsDB.authors select new Author { au_id = p.au_id, au_name = a.au_fname + " " + a.au_lname}).ToList();
}

return result;
}

That should get you started and provide some hints to the general patterns.






Re: LINQ Project General How to return the entity objects beyond the DataContext?

nakama

Hi, Anders. Thanks for your quick reply.
I checked it, the key point is not the usage of anonymous type, but whether to call the .ToList() method or not.


Code Snippet

static void Main(string[] args)
{
var authors = ReturnSample1();
foreach (var a in authors) Console.WriteLine(a);
}

static System.Collections.IEnumerable ReturnSample1()
{
using (PubsDataContext pubsDB = new PubsDataContext())
{
// OK (it works fine)
//var authors = (from a in pubsDB.authors
// select new Author() { au_id = a.au_id, au_name = a.au_fname + " " + a.au_lname }).ToList();
// OK (it works fine)
var authors = (from a in pubsDB.authors
select new { au_id = a.au_id, au_name = a.au_fname + " " + a.au_lname }
).ToList();
// NG (it doesn't work)
//var authors = from a in pubsDB.authors
// select new Author() { au_id = a.au_id, au_name = a.au_fname + " " + a.au_lname };
return authors;
}
}

public class Author
{
public string au_id { get; set; }
public string au_name { get; set; }
}

I already understand the reasons of this difference of behaviors, but I wonder what the guideline of LINQ to SQL should be. How do you think of these guideline

1) If we want to return the entity data, we should implement a concrete type that provides the interface for the caller, not to use anonymous type.
2) If we want to return the collection of entity data, we should implement it by List. (or IEnumerable )
3) If we want to return the results of LINQ-to-SQL, we should call .ToList() method explicitly.

Thanks.





Re: LINQ Project General How to return the entity objects beyond the DataContext?

jwooley

Using ToList/ToArray/etc. is the way to force loading of the results and then return them. You can return an anonymous type as an IEnumerable (not IEnumerable<T>). What you are trying to do in your client may or may not benefit from that return type. In your original example, you are returning an object of IQueryable rather than IEnumerable. With IQueryable, you are trying to return the structure of your query rather than just the results of the query. If you are not planning on re-attaching the client to the database and continue composing additional query methods on your returned definition, you shouln't be returning IQueryable.

Jim Wooley

http://linqinaction.net

http://devauthority.com/blogs/jwooley






Re: LINQ Project General How to return the entity objects beyond the DataContext?

nakama

Sorry for my late reply, and thanks for your sincerly reply!
I understood the usage of IQueryable and IEnumerable!

Thanks!

Nobuyuki Akama