Provides functionality to evaluate queries against a specific data source wherein the type of the data is not specified.
IQueryable, which adds two key methods: CreateQuery<T> and Execute<T>. Both of these methods accept an Expression, which is basically a new set of classes that allow you to express things like method calls, operations, lambda functions, etc, as a tree of expressions.
By using IQueryable you can look inside of the LINQ query, and do things with the expression tree, perhaps so your operation now uses the index. The user now gets the benefit of the index, without even needing to know about it!
Best suitable for:
- To query data from out-memory
- For remote data source - a database or web service (or remote queries)
- IQueryable is suitable for LINQ to SQL queries, it has query provider (e.g., a LINQ to SQL provider)
- Can traverse forward and backward in collection as well as between collection.
- Query expression representing the IQueryable<T> object as a runtime-traversable expression that can be understood by the given query provider
- A "queryable" that is a query you could still be enriched before really sending it to the server.
When to use:
- Use IQueryable if you want to return a base query that could be further enhanced before running it server side (by enumerating its items).
- Derives from IEnumerable
- IQueryable can move forward, backward and between the items.
- Can move items in collection.
- Perform faster search.
- IQueryable execute select query on server side with all filters.
- Support indexing by default.
How it works:
- While query data from database, IQueryable execute select query on server side and filter data on server it self.
- Faster that IEnumerable
When to use:
- When querying data from out-memory (like remote database, service) collections.
Deferred Execution :
- IQueryable supports deferred execution
- IQueryable supports custom query using CreateQuery and Execute methods.
- IQueryable support lazy loading. Hence it is suitable for paging like scenarios.
- Extension methods supports by IQueryable takes expression objects means expression tree.
MyDataContext dc = new MyDataContext (); IQueryable list = dc.Employees.Where(p => p.Name.StartsWith("S")); list = list.Take(10);
SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0
Again - Whatever IEnumerable can do, IQueryable can also do.