• Agregat amallari Linq to Entities
  • Minimal, maksimal va orta qiymatlar Minimal, maksimal va orta qiymatni aniqlash uchun mos ravishda Min() , Max() va Average()
  • Kesishma Ikkita to’plamni kesishmasini aniqlash uchun Intersect()




    Download 1,39 Mb.
    bet31/53
    Sana30.11.2023
    Hajmi1,39 Mb.
    #108476
    1   ...   27   28   29   30   31   32   33   34   ...   53
    Bog'liq
    Dasturiy injiniring

    Kesishma
    Ikkita to’plamni kesishmasini aniqlash uchun Intersect() metodidan foydalaniladi:
    using (PhoneContext db = new PhoneContext())
    {
    var phones = db.Phones.Where(p => p.Price < 25000)
    .Intersect(db.Phones.Where(p => p.Name.Contains("Samsung")));
    foreach (var item in phones)
    Console.WriteLine(item.Name);
    }


    Farq
    Agar birinchi to’plamdan ikkinchi to’plamda mavjud bo’lmagan elementlarni aniqlashimiz lozim bo’lsa, Except metodidan foydalanamiz:
    using (PhoneContext db = new PhoneContext())
    {
    var selector1 = db.Phones.Where(p => p.Price > 25000); // Samsung Galaxy S4, Samsung Galaxy S4, iPhone S4
    var selector2 = db.Phones.Where(p => p.Name.Contains("Samsung")); // Samsung Galaxy S4, Samsung Galaxy S4
    var phones = selector1.Except(selector2); // natija - iPhone S4

    foreach (var item in phones)


    Console.WriteLine(item.Name);
    }
    Agregat amallari
    Linq to Entities orqali SQL ning ichki funksiyalariga Count, Sum va boshqa maxsus metodlar orqali murojaat qilishimiz mumkin.


    Tanlashdagi elementlar soni
    Count() metodi orqali tanlashdagi elementlar sonini aniqlash mumkin:
    using (PhoneContext db = new PhoneContext())
    {
    // jami telefon modellari soni
    int number1 = db.Phones.Count();
    // Samsung qiymatini o’zida saqlagan modellar sonini aniqlaymiz
    int number2 = db.Phones.Count(p => p.Name.Contains("Samsung"));

    Console.WriteLine(number1);


    Console.WriteLine(number2);
    }


    Minimal, maksimal va o'rta qiymatlar
    Minimal, maksimal va o'rta qiymatni aniqlash uchun mos ravishda Min(), Max() va Average() funksiyalardan foydalaniladi. Model bo’yicha minimal, maksimal va o'rta qiymatni aniqlaymiz:
    using (PhoneContext db = new PhoneContext())
    {
    // minimal narx
    int minPrice = db.Phones.Min(p => p.Price);
    // maksimal narx
    int maxPrice = db.Phones.Max(p => p.Price);
    // Samsung firmasi telefonlarining o’rtacha narxi
    double avgPrice = db.Phones.Where(p => p.Company.Name == "Samsung").Average(p => p.Price);

    Console.WriteLine(minPrice);


    Console.WriteLine(maxPrice);
    Console.WriteLine(avgPrice);
    }



    Download 1,39 Mb.
    1   ...   27   28   29   30   31   32   33   34   ...   53




    Download 1,39 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Kesishma Ikkita to’plamni kesishmasini aniqlash uchun Intersect()

    Download 1,39 Mb.