Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the return type of a LINQ query?

Tags:

c#

linq

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?

like image 234
Deepanjan Nag Avatar asked Jun 07 '11 08:06

Deepanjan Nag


People also ask

What do most LINQ operators return?

These operators return a Boolean value i.e. True or False when some or all elements within a sequence satisfy a specific condition.

Does LINQ return reference?

Making a new object that is a reference type is non-trivial. LINQ would have no idea how to do it. LINQ always returns the same instances when dealing with reference types.

Which LINQ method returns a Boolean value?

The All method of System. LINQ. Queryable class returns a Boolean value if all the elements of the sequence satisfy the provided condition. It returns true if all the elements satisfy the condition otherwise it returns false.


2 Answers

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.

like image 133
Dan Abramov Avatar answered Sep 19 '22 22:09

Dan Abramov


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>

like image 38
devio Avatar answered Sep 22 '22 22:09

devio