C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet731/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   727   728   729   730   731   732   733   734   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

let

 statement, 



x

 is set equal to the average of the values in 



nums

. This value is 

obtained by calling 

Average( )

 on 


nums

.

www.freepdf-books.com




580

 

P a r t   I :  



T h e   C #   L a n g u a g e

Defe

rred vs. Immediate Query Execution

In LINQ, queries have two different modes of execution: immediate and deferred. As 

explained early in this chapter, a query defines a set of rules that are not actually executed 

until a 

foreach

 statement executes. This is called 



deferred execution.

However, if you use one of the extension methods that produces a nonsequence result, 

then the query must be executed to obtain that result. For example, consider the 

Count( )

method. In order for 



Count( )

 to return the number of elements in the sequence, the query 

must be executed, and this is done automatically when 

Count( )

 is called. In this case, 



immediate execution

 takes place, with the query being executed automatically in order to 

obtain the result. Therefore, even though you don’t explicitly use the query in a 

foreach

loop, the query is still executed.

Here is a simple example. It obtains the number of positive elements in the sequence.

// Use immediate execution.

using System;

using System.Linq;

class ImmediateExec {

  static void Main() {

    int[] nums =  { 1, -2, 3, 0, -4, 5 };

    // Create a query that obtains the number of positive

    // values in nums.

    int len = (from n in nums

               where n > 0

               select n).Count();

    Console.WriteLine("The number of positive values in nums: " + len);

  }


}

The output is

The number of positive values in nums: 3

In the program, notice that no explicit 



foreach

 loop is specified. Instead, the query 

automatically executes because of the call to 

Count( )

.

As a point of interest, the query in the preceding program could also have been written 



like this:

var posNums = from n in nums

              where n > 0

              select n;

int len = posNums.Count(); // query executes here

In this case, 



Count( )

 is called on the query variable. At that point, the query is executed to 

obtain the count.

Two other methods that cause immediate execution of a query are 



ToArray( )

 and 


ToList( )

. Both are extension methods defined by 



Enumerable

.

ToArray( )

 returns the results 

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   727   728   729   730   731   732   733   734   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish