Parmenides (3) [Avatar] Offline
#1
How necessary is IQueryable in LINQ to SQL?
I don't fully understand the statement which says that the compiler retains steps to create a query...."IQueryable's provider implementation can translate the language construct into a format understood by the underling data source.".... "adding more functionality (ordering, grouping, aggregating, pagin)..." (254-255)

Concerning the above capabilities of IQueryable, a plain IEnumerable list seems to implement that fine. For example:

DataContext db = new DataContext();
IEnumerable<customclass> NonIQueryableList= new List<customclass>();

NonIQueryableList = (from a in db.table
where....
select new customclass { ID=a.ID, Name=a.Name }).Take(10)

SQL takes the whole statement, even the paging. However, it wouldn't if I paged in a separate statement (ex. QueryList.Take(10) ).

I don't see the significance of IQueryable, except perhaps more advanced situations, or perhaps the ability to call a paging method (among other things) on the collection at a later time in the code while allowing SQL to page for us.

Is there any light to shed on this?
Parmenides (3) [Avatar] Offline
#2
Re: IQueryable vs. IEnumerable 7:2.1 page 254-255
At the risk of sounding too vague, let me clarify:

I don't seem to need an IQueryable list to translate the methods into SQL language. SQL profiler has shown me that it works fine when using IEnumerable. The only time IQueryable would be needed for the list was if I were to modify that list at a later time. Is this correct?
Lee Dumond (29) [Avatar] Offline
#3
Re: IQueryable vs. IEnumerable 7:2.1 page 254-255
I'm not sure what you're trying to get at here.

IQueryable is absolutely necessary to querying remote data stores. In fact, the example you provide is actually of type Queryable<T> all the way up to the last method call (Take(10)), at which point it becomes an Enumerable<T>.

If you look at your example closely, you will see that the method you are calling is NOT Enumerable<T>.Take(), but is actually Queryable<T>.Take(), which then returns an Enumerable<T> result.

The confusion may arise from the fact that System.Linq.Enumerable and System.Linq.Queryable contain methods with the same names.
Parmenides (3) [Avatar] Offline
#4
Re: IQueryable vs. IEnumerable 7:2.1 page 254-255
Thank you. That clarifies things. I guess part of my confusion arose from the book's seeming statement that Enumerable<T> can't translate the method .take(10) into SQL. But what you said makes sense now.