17 Ocak 2014 Cuma

LINQ ile kayıtlarda SUM, MAX,MIN kullanımı

1. Formumuz üzerine bir tane datagridview bir tane button ekleyelim. 
2. F7 tuşuna basarak kod yazım ekranına geçelim.
3. Aşağıdaki şekilde bir class yazalım.
        public class Kayit
        {
            public string isim { get; set; }
            public string dyeri { get; set; }
            public string cinsiyeti { get; set; }
            public int yasi { get; set; }
        }
4. Form üzerindeki butonumuzu tıklayıp içine
           //öncelikle Kayit tipinde bir Generic List tanımlayıp içerisini aşağıdaki şekilde dolduruyoruz.
            List<Kayit>  KayitListesi = new List<Kayit>
            { 
                new Kayit {isim = "Ali", dyeri = "Bursa", cinsiyeti = "Erkek", yasi = 20},
                new Kayit {isim = "Ayşe", dyeri = "İzmir", cinsiyeti = "Bayan", yasi = 10},
                new Kayit {isim = "Veli", dyeri = "Bursa", cinsiyeti = "Erkek", yasi = 30},
                new Kayit {isim = "Cem", dyeri = "Bursa", cinsiyeti = "Erkek", yasi = 15},
                new Kayit {isim = "Can", dyeri = "Bursa", cinsiyeti = "Erkek", yasi = 18},
                new Kayit {isim = "Mine", dyeri = "İzmir", cinsiyeti = "Bayan", yasi = 18},
                new Kayit {isim = "Oya", dyeri = "Bursa", cinsiyeti = "Bayan", yasi = 11},
                new Kayit {isim = "Metin", dyeri = "Bursa", cinsiyeti = "Erkek", yasi = 23},
                new Kayit {isim = "Ayça", dyeri = "Bursa", cinsiyeti = "Bayan", yasi = 25},
                new Kayit {isim = "Ayşe", dyeri = "Van", cinsiyeti = "Bayan", yasi = 34}
            };
            var liste = from x in KayitListesi select x.yasi;
            MessageBox.Show(liste.Sum().ToString());
            MessageBox.Show(liste.Min().ToString());
            MessageBox.Show(liste.Max().ToString());
            MessageBox.Show(liste.Count().ToString());
            MessageBox.Show(liste.Average().ToString());
 
 

Hiç yorum yok:

Yorum Gönder