Deepanjan Nag
Deepanjan Nag

Reputation: 921

What's the return type of a LINQ query?

Is it IEnumerable<T>. As far as I know, the reference always points to a class instance. What instance type does the LINQ query really point to?

Upvotes: 28

Views: 31820

Answers (5)

Sanu Uthaiah Bollera
Sanu Uthaiah Bollera

Reputation: 937

You can check here for Three parts of Query operation.You can see that the return type of an LINQ query is IEnumerable< int >.

Upvotes: -1

Dan Abramov
Dan Abramov

Reputation: 268225

You can find it out by calling .GetType() on your IEnumerable<T> variable and inspecting the type in the debugger.

For different LINQ providers and even different LINQ methods, such types may or may not be different.

What matters to your code is that they all implement IEnumerable<T> which you should work with, or IQueryable<T> which also accepts expressions, meaning your predicates and projections will become syntax trees and may be manipulated by a LINQ provider at runtime, e.g. to be translated into SQL.

Actual classes, if this is what you're asking about, may even be compiler-generated, e.g. yield return expression is translated to such a class.
Either way, they are usually internal and you should never, ever depend on them.

Upvotes: 18

devio
devio

Reputation: 37205

Depending on your original data source, it is either IEnumerable or IQueryable:

The result of a Linq database query is typically IQueryable<T> which is derived from IEnumerable<T>, IQueryable, and IEnumerable.

If your database query includes an OrderBy clause, the type is IOrderedQueryable<T>, being derived from IQueryable<T>

If your data source is an IEnumerable, the result type is IEnumerable<T>

Upvotes: 10

Soul Reaver
Soul Reaver

Reputation: 2092

if I am not mistaken, IEnumerable< T >, where T depends on your query

Upvotes: 0

helios
helios

Reputation: 13841

I don't know but I guess it's an internal type. You don't have to think about the class.

In fact it could be different classes depending on the concrete query. The compiler could convert the LINQ expression to one or another implementation depending on the conditions/processing.

Upvotes: 3

Related Questions