5-mavzu:LINQ operatorlari
LINQ yordamida ma’lumotlarni filtrlash masalasini ko’rib chiqamiz.Ma’lumotlar to’plamidan biror shart asosida elementlrini filtrlab olish uchun Where operatorini qo’llaymiz.Masalan:10 dan kata bo’lgan,berilgan to’plamdan,barcha juft elementlarni filtrlashni LINQ yordamida ko’rib chiqamiz:
Int [] numbers={1,2,3,4,10,34,55,66,77,88};
IEnumerable evens=from i in
numbers where i%2==0
&& i>10 selecti;
Foreach (int I in evens)
Console.Writeline(i);
Yuqoridagi LINQ so’rovini quyidagi ko’rinishda ham yozish mumkin:
Int [] numbers={1,2,3,4,10,34,55,66,77,88};
IE numerable evens-numbers.
Where(i=>i%2==0 && i>10);
Endi murakkabroq obyektlar ustida filtrlash masalasini ko’rib chiqamiz.Faraz qilaylik bizga foydalanuvchilar to’g’risida yaratilgan sinf berilgan bo’lsin.
Class User
{
Public string name {get;set;}
Public int Age {get;set;}
Public List Languages {get;set;}
Public User()
{
Languages=newList();
}
}
Shu sinf asosida foydalanuvchilar ro’yhatini yaratamiz va shu ro’yhatdan Yoshi 25 yoshdan kata bo’lgan foydalanuvchilarni Console ga chiqarish dasturini tuzamiz:
List users=newList
{
New User {Name=”Tom”,Age=23,Languages=newList
{ “angliyskiy”,”nemiskiy”}},
New User {Name=”Bob”,Age=27,Languages=new List
{“angliyskiy”,”fransuskiy”}}
New User {Name=”Jon”,Age=29,Languages=new List
{“angliyskiy”,”ispanskiy’}}
New User {Name=”Elis”,Age=24,Languages=newList
{“ispanskiy”,”nemiskiy”}}
};
Var selectedusers=from user in users
where user.Age>25
select user;
Foreach (user user in selectedUsers)
Console.WriteLine{$”{User.name}-{User.Age}”);
Natija :
Bob=27
Jon=29
Yuqoridagi so’rovga teng kuchli bo’lgan quyidagi so’rovni yozamiz:
Var selectedUsers=users.Where
(u=>u.Age>25);
Murakkab filtrlash
Murakkab filtrlarni yaratishni qaraylik.Masalan foydalanuvchilar ro’yhatida quyidagi shart bilan mavjud bo’lganlarini aniqlaymiz.
Var selectedusers=fromUsers=from user in users
from long in user.Languages
where user.Age<28
Where lang==”angliyskiy”
Select user;
Natija:
Tom=23
Bob=27
Yuqoridagi so’rovga mos bo’lgan quyidagi kengaytirilgan so’rovni SelectMany yordamida yozamiz.
Var selectedUsers=users.SelectMany
(u=>u.Languages,
(u,l)=>new{User=4,Lang=l}}
.Where9u=>u.Lang==”angliyskiy”&&u.User.Age<28)
>Select(u=>u.User);
Proyeksiya
LINQ yordamida ma’lumotlardan kerakli tiplar yordamida proyeksiyalarni (nusxalarni) Select dan foydalanib yaratish mumkin.Faraz qilaylik bizda quyidagi sinf yordamida yaratilgan obyektlar to’plami mavjud bo’lsin.
Class User
{
Public string Name {get;set:}
Public intAge {get;set;]
}
Bizga to’liq obyekt emas,faqatgina name xossasi kerak bo’lsin.
List users=new List ();
Users.Add(newUser {Name=”Sam”,Age=43}};
Users.Add(new User{Name=”Tom”,Age=33}};
Var names=from u in users select u.Name;
Foreach(string n in names)
Console.WriteLine(n);
Misolni natijasi sifatida Name xossasini qiymati bo’ladi.Bu usul yordamida (proyeksiyalash) yangi obyektlarni ham yaratish mumkin.
List users=newList();
Users.Add(new User {Name=”Sam”,Age=43});
Users.Add(new User {Name=”Tom”,Age=33});
Var items=from u in users
Select new
{
FirstName=u.Name,
DateOfBirth=DateTime.Now.Year-u.Age};
Foreach (var n in items)
Console.WriteLine($.FirstName}-{n.DateOfBirth}”);
Yuqorida items nomli yangi object yaratiladi va DateOF.Birth xossalari ham yaratiladi.
Yuqoridagi kodni kengaytirilgan usul yordamida quyidagicha ham yozish mumkin.
Var names=users.Select(u=>u.Name);
Var items =users.Select (u=>new
{
FirstName=u.Name,
DateOfBirthDateTime.Now.Year-u.Age]);
So’rovlarda o’zgaruvchilarni ham ishlatish mumkin.bu holatdan oraliq hisob-kitoblarni amalga oshirishda foydalaniladi.Va biz uni Let operatori yordamida amalga oshiramiz. 28>
Do'stlaringiz bilan baham: |