C# 0 The Complete Reference


More Query-Related Extension Methods



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

More Query-Related Extension Methods

In addition to the methods that correspond to the query keywords supported by C#, the 

.NET Framework provides several other query-related extension methods that are often 

helpful in a query. These query-related methods are defined for 



IEnumerable

 by 


Enumerable

. Here is a sampling of several commonly used methods. Because many of the 

methods are overloaded, only their general form is shown.

www.freepdf-books.com




578

 

P a r t   I :  



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

Method


Description

All(


condition

)

Returns true if all elements in a sequence satisfy a specified condition.



Any(

condition

)

Returns true if any element in a sequence satisfies a specified condition.



Average( )

Returns the average of the values in a numeric sequence.

Contains(

obj

)

Returns true if the sequence contains the specified object.



Count( )

Returns the length of a sequence. This is the number of elements that it 

contains.

First( )


Returns the first element in a sequence.

Last( )


Returns the last element in a sequence.

Max( )


Returns the maximum value in a sequence.

Min( )


Returns the minimum value in a sequence.

Sum( )


Returns the summation of the values in a numeric sequence.

You have already seen 



Count( )

 in action earlier in this chapter. Here is a program that 

demonstrates the others:

// Use several of the extension methods defined by Enumerable.

using System;

using System.Linq;

class ExtMethods {

  static void Main() {

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

    Console.WriteLine("The minimum value is " + nums.Min());

    Console.WriteLine("The maximum value is " + nums.Max());

    Console.WriteLine("The first value is " + nums.First());

    Console.WriteLine("The last value is " + nums.Last());

    Console.WriteLine("The sum is " + nums.Sum());

    Console.WriteLine("The average is " + nums.Average());

    if(nums.All(n => n > 0))

      Console.WriteLine("All values are greater than zero.");

    if(nums.Any(n => (n % 2) == 0))

      Console.WriteLine("At least one value is even.");

    if(nums.Contains(3))

      Console.WriteLine("The array contains 3.");

  }


}

The output is shown here:

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   725   726   727   728   729   730   731   732   ...   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