Mavzu: linq so’rovlar. Reja: I. Kirish II. Asosiy qism Parallel linq. Parallel linq ga kirish. AsParallel usuli. AsOrdered usuli


int[] numbers = new int[] { -2, -1, 0, 1, 2, 4, 3, 5, 6, 7, 8, }


Download 48.13 Kb.
bet3/6
Sana18.06.2023
Hajmi48.13 Kb.
#1590764
1   2   3   4   5   6
Bog'liq
18-Mavzu- LINQ so’rovlar

int[] numbers = new int[] { -2, -1, 0, 1, 2, 4, 3, 5, 6, 7, 8, };
(from n in numbers.AsParallel()
where n >0
select Factorial(n))
.ForAll(Console.WriteLine);

ForAll() usuli parametr sifatida Action delegatini oladi, bu bajariladigan amalni bildiradi.




2. AsOrdered usuli

Parallel so'rovni bajarishda natijalar to'plamidagi ma'lumotlar tartibini oldindan aytib bo'lmaydi. Masalan:




static void Main(string[] args)
{
int[] numbers = new int[] { -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, };
var factorials = from n in numbers.AsParallel()
where n >0
select Factorial(n);
foreach (var n in factorials)
Console.WriteLine(n);
Console.Read();
}
static int Factorial(int x)
{
int result = 1;
for (int i = 1; i <= x; i++)
{
result *= i;
}
return result;
}
Dastur bizga quyidagi natijalarni berishi mumkin:


5040
1
40320
2
24
6
120
720

Ya'ni, ma'lumotlar tartibsiz ravishda umumiy to'plamga biriktirilgan.Agar so'rovda so'rovda saralash operatorlari yoki usullari ishlatilsa, ma'lumotlar avtomatik ravishda saralanadi:


var factorials = from n in numbers.AsParallel()
where n >0
orderby n
select Factorial(n);

Biroq, orderby operatori yoki OrderBy usuli har doim ham so'rovlarda ishlatilmaydi. Bundan tashqari, ular olingan namunani asl ketma-ketlikka ko'ra emas, balki natijalarga ko'ra buyruq beriladi. Bunday hollarda biz AsOrdered() usulidan foydalanishimiz mumkin:





Download 48.13 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6




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