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:
Do'stlaringiz bilan baham: |