The difference between LINQ statement .AsEnumerable () and .AsQueryable () of

When writing LINQ statement, you tend to see .AsEnumerable () and .AsQueryable ().
E.g:

Copy the code
Copy the code
STRCON = String "the Data = \\ SQLEXPRESS the Source;. = Db_Example the Initial Cataog; the Persist Security Info = True; the User ID = SA; Password = SA"; 
the SqlConnection the SqlConnection new new = CON (STRCON);
con.Open ();
String strSQL = "SELECT * from SC, Course, WHERE SC.Cno = Course.Cno";
the SqlDataAdapter new new DA = the SqlDataAdapter (strSQL, CON);
the DataSet the DataSet new new DS = ();
da.Fill (DS, "MyTable");
the DataTable Tables = ds.Tables [ "mytable"]; // create a table
var DSLP = from d in the tables .AsEnumerable () the SELECT d; // execute LINQ statement here .AsEnumerable () is the delay occurred, is not performed immediately, the actual nothing happened on
the foreach (RES in DSLP var)
{
Response.Write (res.Field <String> ( "the Cname") the ToString ().);
}
Copy the code
Copy the code

       The code above uses LINQ filter and sort the data for the data set, the data set can likewise be screened data in an object-oriented thinking. When using LINQ data set operations, LINQ not query directly from the data set object, the object does not support the data set because LINQ queries, it is necessary to use a method to return AsEnumerable generic objects to support LINQ query operation.

.AsEnumerable () execution is delayed, in fact, nothing happened, when the real object is used (for example, call: First, Single, ToList .... time) before execution.
Here is .AsEnumerable () corresponding to the difference between .AsQueryable () is:
AsEnumerable an up-converted into a sequence IEnumerable, the binding force Enumerable following query operator class to which the subsequent sub-queries.
AsQueryable a sequence into a downward IQueryable, it generates a local query IQueryable packaging.

  • .AsEnumerable () delay the execution, it is not performed immediately. When you call .AsEnumerable (), in fact, nothing happened.
  • .ToList () executed immediately
  • When you need to manipulate the results, with .ToList (), otherwise, if only for the query result set no further use, and can delay the execution, use .AsEnumerable () / IEnumerable / IQueryable
  • .AsEnumerable () Although the delay in the implementation, but still access the database, and .ToList () directly to obtain results in memory. For example, we need to show employees of the two departments, the department can first be placed out in the List, and then turn out employees of various departments, the efficiency is higher then visit because there is no access to the database each time to remove the department.
  • IQueryable implements IEnumberable interface. However IEnumerable <T> into IQueryable <T> After much faster. the reason:
  • The difference between IQueryable interface IEnumberable interfaces: IEnumerable <T> generic class in their calling and Take SKip and other extension methods before the data has been loaded in the local memory, while the IQueryable <T> is Skip, take these methods expressions after sending commands translated into T-SQL statements SQL server again, it is not all the data are loaded into memory only to conditional filtering.
  • IEnumerable is running Linq to Object, forced to read all the data from the database into memory first.

When writing LINQ statement, you tend to see .AsEnumerable () and .AsQueryable ().
E.g:

Copy the code
Copy the code
STRCON = String "the Data = \\ SQLEXPRESS the Source;. = Db_Example the Initial Cataog; the Persist Security Info = True; the User ID = SA; Password = SA"; 
the SqlConnection the SqlConnection new new = CON (STRCON);
con.Open ();
String strSQL = "SELECT * from SC, Course, WHERE SC.Cno = Course.Cno";
the SqlDataAdapter new new DA = the SqlDataAdapter (strSQL, CON);
the DataSet the DataSet new new DS = ();
da.Fill (DS, "MyTable");
the DataTable Tables = ds.Tables [ "mytable"]; // create a table
var DSLP = from d in the tables .AsEnumerable () the SELECT d; // execute LINQ statement here .AsEnumerable () is the delay occurred, is not performed immediately, the actual nothing happened on
the foreach (RES in DSLP var)
{
Response.Write (res.Field <String> ( "the Cname") the ToString ().);
}
Copy the code
Copy the code

       The code above uses LINQ filter and sort the data for the data set, the data set can likewise be screened data in an object-oriented thinking. When using LINQ data set operations, LINQ not query directly from the data set object, the object does not support the data set because LINQ queries, it is necessary to use a method to return AsEnumerable generic objects to support LINQ query operation.

.AsEnumerable () execution is delayed, in fact, nothing happened, when the real object is used (for example, call: First, Single, ToList .... time) before execution.
Here is .AsEnumerable () corresponding to the difference between .AsQueryable () is:
AsEnumerable an up-converted into a sequence IEnumerable, the binding force Enumerable following query operator class to which the subsequent sub-queries.
AsQueryable a sequence into a downward IQueryable, it generates a local query IQueryable packaging.

  • .AsEnumerable () delay the execution, it is not performed immediately. When you call .AsEnumerable (), in fact, nothing happened.
  • .ToList () executed immediately
  • When you need to manipulate the results, with .ToList (), otherwise, if only for the query result set no further use, and can delay the execution, use .AsEnumerable () / IEnumerable / IQueryable
  • .AsEnumerable () Although the delay in the implementation, but still access the database, and .ToList () directly to obtain results in memory. For example, we need to show employees of the two departments, the department can first be placed out in the List, and then turn out employees of various departments, the efficiency is higher then visit because there is no access to the database each time to remove the department.
  • IQueryable implements IEnumberable interface. However IEnumerable <T> into IQueryable <T> After much faster. the reason:
  • The difference between IQueryable interface IEnumberable interfaces: IEnumerable <T> generic class in their calling and Take SKip and other extension methods before the data has been loaded in the local memory, while the IQueryable <T> is Skip, take these methods expressions after sending commands translated into T-SQL statements SQL server again, it is not all the data are loaded into memory only to conditional filtering.
  • IEnumerable is running Linq to Object, forced to read all the data from the database into memory first.

Guess you like

Origin www.cnblogs.com/longyong007/p/12567677.html
Recommended