Default return type of LINQ Query [Resolved]

Posted by Sriramramesh1610-23884 under LINQ on 6/8/2016 | Points: 10 | Views : 826 | Status : [Member] | Replies : 5
What is the default return type of LINQ Query? please explain?




Responses

Posted by: A2H on: 6/8/2016 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
AFAIK, its depends on the select operation
If you are querying a database(Linq to SQL) then the return type is IQueryable<T> where T is Product in below example
var product = from p in dbContext.Products
select p;


If you are using Linq againist a datasource which is an Enumerable collection(List) then the return type will be IEnumerable<t>

Additional Read : https://blogs.msdn.microsoft.com/wriju/2008/05/06/linq-ienumerablet-and-iqueryablet/

Thanks,
A2H
My Blog

Sriramramesh1610-23884, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 6/9/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Sriramramesh1610-23884 Sir, as rightly mentioned by @A2H, it depends on your original data source. It can be either IEnumerable or IQueryable

The result of a Linq database query is typically IQueryable<T> .

If the database query includes an OrderBy clause, the type is IOrderedQueryable<T> .It is derive from IQueryable<T>

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

You can find out the datatype by calling .GetType() on the source.

Hope this helps.

--
Thanks & Regards,
RNA Team

Sriramramesh1610-23884, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sriramramesh1610-23884 on: 6/9/2016 [Member] Starter | Points: 25

Up
0
Down
thank you guys.

Sriramramesh1610-23884, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 6/9/2016 [Member] [MVP] Silver | Points: 25

Up
0
Down
Glad to be of help :)

Thanks,
A2H
My Blog

Sriramramesh1610-23884, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muneshs16 on: 6/11/2016 [Member] Starter | Points: 25

Up
0
Down
ou 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.

Munesh

Sriramramesh1610-23884, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response