C# 0 The Complete Reference


Create Queries by Using the Query Methods



Download 4,07 Mb.
Pdf ko'rish
bet724/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   720   721   722   723   724   725   726   727   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Create Queries by Using the Query Methods

By using the query methods in conjunction with lambda expressions, it is possible to create 

queries that do not use the C# query syntax. Instead, the query methods are called. Let’s 

begin with a simple example. It reworks the first program in this chapter so that it uses calls 

to

Where( )

 and 


Select( )

 rather than the query keywords.

// Use the query methods to create a simple query.

// This is a reworked version of the first program in this chapter.

using System;

using System.Linq;

class SimpQuery {

  static void Main() {

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

    // Use Where() and Select() to create a simple query.

    var posNums = nums.Where(n => n > 0).Select(r => r);

    Console.Write("The positive values in nums: ");

    // Execute the query and display the results.

    foreach(int i in posNums) Console.Write(i + " ");

    Console.WriteLine();

  }


}

The output, shown here, is the same as the original version:

The positive values in nums: 1 3 5

In the program, pay special attention to this line:

var posNums = nums.Where(n => n > 0).Select(r => r);

This creates a query called 



posNums

 that creates a sequence of the positive values in 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   720   721   722   723   724   725   726   727   ...   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