C # IEnumerable and IQueryable, IEnumerable and IList, LINQ to understand Var and IEnumerable

Original: https: //www.cnblogs.com/WinHEC/articles/understanding-var-and-ienumerable-with-linq.html

So when using LINQ to query data from the database and collections, we use IEnumerable and IQueryable data processing. IQueryable inherits from IEnumerable, IQueryable therefore has all the features IEnumerable, in addition, it also has its own function. Both have their importance to query and manipulate data. Let's look at the advantages of both, and use their strengths to enhance your LINQ Query performance.

IEnumerable

  1. IEnumerable exists in the System.Collections namespace.

  2. IEnumerable can only move forward on the set, it can not move backward and move between Items.

  3. IEnumerable preferably from memory a set of query data, such as List, Array like. When the data from a database query, IEnumerable executed on the server select query, and then load the data in memory on the client, and finally filtered data.

  4. IEnumerable applies to LINQ to Object and LINQ to XML queries.

  5. IEnumerable support deferred execution.

  6. IEnumerable does not support custom queries, do not support lazy loading. Therefore not suitable scene similar to paging.

  7. IEnumerable support the expansion method to obtain practical object.

IEnumerable example:

 

MyDataContext dc = new MyDataContext ();
IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10); 

 

The above query will generate the following SQL statement:

SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] 
FROM [Employee] AS [t0]
WHERE [t0].[EmpName] LIKE @p0

Note: Missing [TOP 10] In this query statement, because IEnumerable is filtering data on the client side.

 

 

 

IQueryable

  1. System.Linq IQueryable exist in the namespace.

  2. IQueryable can only move forward on the set, it can not be moved backwards and move between projects.

  3. IQueryable best from the non-memory query data (such as remote databases, services, etc.) collection. When data from a database query, IQueryable execute a SELECT query with all filters on the server side.

  4. IQueryable applies to LINQ to SQL queries.

  5. IQueryable support deferred execution.

  6. IQueryable support the use of CreateQuery and Execute methods of custom queries.

  7. IQueryable support lazy loading. Therefore, it is suitable for a similar paging scene.

  8. IQueryable extension methods to support the use of expression object, expression trees.

IQueryable example:

1  MyDataContext dc = new MyDataContext ();
2 IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
3 list = list.Take<Employee>(10); 

The above query will generate the following SQL statement:

SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
WHERE [t0].[EmpName] LIKE @p0

Note: In this query statement [TOP 10] exist, because IQueryable is executed in SQLSERVER with filter conditions in SQL statements.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

IList

  1. IList present in the System.Collections namespace.

  2. IList used to access the list of elements specific location / index.

  3. Like like IEnumerable, IList it's best to query data from a set of lists, arrays and other memory.

  4. When you want to add or remove items in the list, IList useful.

  5. IList can find out the number of elements in the collection without iteration set.

  6. IList support deferred execution.

  7. IList does not support further filtration.

 

IEnumerable

  1. IEnumerable exists in the System.Collections namespace.

  2. IEnumerable can only move forward on the set, it can not move backward and move between Item.

  3. IEnumerable query data from a collection of the best lists, arrays and other memory.

  4. IEnumerable does not support the add or remove items from the list.

  5. Use IEnumerable, we can find out the number of elements in the collection after collection of iterations.

  6. IEnumerable support deferred execution.

  7. IEnumerable support further filtration.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Var is anonymous type, so even though you do not know the type of output you can use it. In LINQ, suppose you're Join query the two tables, and retrieve data from two tables, the result will be the anonymous type.

Copy the code
1  var q =(from e in tblEmployee 
2  join d in tblDept on e.DeptID equals d.DeptID 
3  select new 
4  { 
5    e.EmpID,
6    e.FirstName,
7    d.DeptName
8  }); 
Copy the code

In the above query, since the results from the two tables, the use of type var.

 

Copy the code
var q =(from e in tblEmployee where e.City=="Delhi" 
select new { 
    e.EmpID,
    FullName=e.FirstName+" "+e.LastName, 
    e.Salary 
}); 
Copy the code

In the above query, the results only from a single table, but we will be the employee's name and last name combinations for the new type FullName, which is anonymous type, so use the Var type. Therefore, when you want to quickly create a "custom" type, use Var type.
More often, var variables like IQueryable as much as it execute a SELECT query with all the filters on the server side. Please refer to the following example to explain.

 

IEnumerable example:

1  MyDataContext dc = new MyDataContext ();
2 IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
3 list = list.Take<Employee>(10); 

The above query will generate the following SQL statement:

1 SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] 
2 FROM [Employee] AS [t0]
3 WHERE [t0].[EmpName] LIKE @p0

Note: Missing [TOP 10] In this query statement, because IEnumerable is filtering data on the client side.

 

Example Var:

 MyDataContext dc = new MyDataContext ();
var list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10); 

The above query will generate the following SQL statement:

SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0

Note: this query is with [TOP 10], because here is the type var IQueryable. SELECT will perform with all the filters in SQLSERVER end.

 

IEnumerable与LINQ

IEnumerable is a collection of forward only when we already know the type of query results is useful. In the following query, the result will be mapped (the employee table) list of employees.

Copy the code
1 IEnumerable<tblEmployee> lst =
2 (
3     from e in tblEmployee 
4     where e.City=="Delhi" 
5     select e
6 ); 
Copy the code

 

to sum up:

  1. In the LINQ query, when you want to "Custom" type, use Var type.

  2. In the LINQ query, when you already know the type of query results, use IEnumerable.

  3. In the LINQ query, Var also applies to the remote collection (database or other services), because it behaves like IQuerable.

  4. IEnumerable apply to the collection in memory.

Guess you like

Origin www.cnblogs.com/zhang1f/p/11233216.html