Let's take a look at the followingCode. Asenumerable ():
1 VaR Query = (From In DB. Table
2 Where A = Somecondition
3 Select a. somenumber). asenumerable ();
4
5 Int Recordcount = Query. Count ();
6 Int Totalsomenumber = Query. sum ();
7 Decimal Average = Query. Average ();
It is not a good task to query the database three times instead of once. If you replace the first row with. tolist (), the database is queried only once.
. Asenumerable () is a delayed execution, but nothing actually happens. when an object is actually used (for example, calling: first, single, tolist .... for example, the following code:
1 Datatable orders = DS. Tables [ " Salesorderheader " ];
2
3 // Query the salesorderheader table for orders placed
4 // After August 8, 2001.
5 Ienumerable < Datarow > Query =
6 From Order In Orders. asenumerable ()
7 Where Order. Field < Datetime > ( " Orderdate " ) > New Datetime ( 2001 , 8 , 1 )
8 Select order;
9
10 // Create a table from the query.
11 Datatable boundtable = Query.Copytodatatable < Datarow > ();
12
13 // Bind the table to a system. Windows. Forms. bindingsource object,
14 // Which acts as a proxy for a system. Windows. Forms. datagridview object.
15 Bindingsource. datasource = Boundtable;
But it should not be abused. tolist (), when you need to operate the results, use. tolist (). Otherwise, if it is only used for query, no further use of the result set is required, and execution can be delayed, it will be used. asenumerable ()/Ienumerable/Iqueryable, for example, the following example:
1 Using (Blogdbcontext Context = New Blogdbcontext ())
2 {
3 VaR result = (From E In Context. blogentries
4 Join t In Context. posttexts
5 On E. ID equals T. ID
6 Where E. ID = Entryid
7 Select New
8 {
9 Title = E. title,
10 Body = T. Text
11 })
12 . Tolist ()
13 . Select (E => New Blogentry () {Title = E. Title, body = E. body })
14 . Firstordefault ();
15 }
The intermediate result in this example is not used, so. tolist () can be changed to. asenumerable ().
. Asenumerable () and. tolist () are different:
- . Asenumerable () is delayed and will not be executed immediately. When you call. asenumerable (), nothing actually happens.
- . Tolist () immediate execution
- When you need the operation results, use. tolist (). Otherwise, if you only need to use the result set for query and can delay execution, use. asenumerable ()/
Ienumerable/Iqueryable
- Although. asenumerable () is delayed, it still accesses the database, while. tolist () directly obtains the result and stores it in the memory. For example, when we need to display the employees of two departments, the departments can be extracted and placed in the list first, and then the employees of each department are retrieved in sequence. In this case, the access efficiency is higher, because you do not need to access the database every time to retrieve the department.
- Iqueryable implements
Ienumberable interface. However, ienumerable <t> is much faster than iqueryable <t>. Cause:
- The iqueryable interface differs from the ienumberable interface: ienumerable <t> generic classes load data in the local memory before calling their own skip and take extension methods, iqueryable <t> is to translate the Skip, take these method expressions into T-SQL statements and then send the command to the SQL server, it does not load all data into the memory for conditional filtering.
- Ienumerable runs the LINQ to object command and forces the system to read all data from the database to the memory first.
1 Namespace System. LINQ
2 {
3 Public Interface Iqueryable: ienumerable
4 {
5 Type elementtype { Get ;}
6
7 Expression expression { Get ;}
8
9 Iqueryprovider { Get ;}
10 }
11
12 Public Interface Iqueryable < Out T > : Ienumerable < T > , Iqueryable, ienumerable
13 {
14 }
15 }