![]()
Şimdi de Linq ile datalarımızı çekerken bazı alanları gruplara ayırmak zorunda kalabiliriz. Gruplama işlemi yaparken de GroupBy() methodunu kullanırız.
GroupBy() : Çektiğimiz datalarda Gruplama işlemi yaparken kullanırız. Mesela notlarına göre öğrencilerin gruplanması , Şehirlere göre insanların gruplanması , kategorilere göre eşyaların gruplanması , Tarihlere göre ödemelerin gruplanması gibi…. bir çok işlerde sıklıkla kullanırız.
Önemli Not : Gruplanan elemanlar IGrouping<TKey, TElement> arayüzünü uygulayan bir koleksiyon olarak dönerler. Buradaki T harfi herhangi bir tipe , TKey de gruplama için kullanılan ortak özelliğin tipine karşılık gelir ve TElement te gruplanan elemanların tipini temsil eder.
Örnek kod
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
List<Student> objStudent = new List<Student>() { new Student() { Id =1,Name = “Suresh Dasari”, Gender = “Female”,Age=20 }, new Student() { Id =2,Name = “Rohini Alavala”, Gender = “Male”, Age=35 }, new Student() { Id =3,Name = “Praveen Alavala”, Gender = “Female”,Age=20 }, new Student() { Id =4,Name = “Sateesh Alavala”, Gender = “Male”, Age =35}, new Student() { Id =5,Name = “Adrian Sai”, Gender = “Male”, Age=35}, new Student() { Id =6,Name = “Alvin Dustin”, Gender = “Male”, Age=35}, new Student() { Id =7,Name = “Axel Sai”, Gender = “Female”, Age=20}, new Student() { Id =8,Name = “Brice Dustin”, Gender = “Female”, Age=20}, }; var studGroupList = objStudent.GroupBy(x => x.Age); foreach (var stud in studGroupList ) { Console.WriteLine(“—-> “ + stud .Key + ” => Yaş grubu”); Console.WriteLine(“——————–“); foreach (var item in stud ) { Console.WriteLine(item.Name + “\t” + item.Gender); } //Output: //—-> 20 => Yaş grubu //——————– //Suresh Dasari Female //Praveen Alavala Female //Axel Sai Female //Brice Dustin Female //—- > 35 => Yaş grubu //——————– //Rohini Alavala Male //Sateesh Alavala Male //Adrian Sai Male //Alvin Dustin Male } class Student { public int Id { get; set; } public string Name { get; set; } public string Gender { get; set; } public int Age { get; set; } } |
Başka bir örnek:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
List<Student> objStudent = new List<Student>() { new Student() { Id =1,Name = “Suresh Dasari”, Gender = “Female”,Age=20 }, new Student() { Id =2,Name = “Rohini Alavala”, Gender = “Male”, Age=35 }, new Student() { Id =3,Name = “Praveen Alavala”, Gender = “Female”,Age=20 }, new Student() { Id =4,Name = “Sateesh Alavala”, Gender = “Male”, Age =35}, new Student() { Id =5,Name = “Adrian Sai”, Gender = “Male”, Age=35}, new Student() { Id =6,Name = “Alvin Dustin”, Gender = “Male”, Age=35}, new Student() { Id =7,Name = “Axel Sai”, Gender = “Female”, Age=20}, new Student() { Id =8,Name = “Brice Dustin”, Gender = “Female”, Age=20}, }; List<Student> queryList = (from d in objStudent group d by d.Age into age select new Student() { Age = age.Key, Students = age.Select(a => a.Name).ToList() }).ToList(); foreach (var item in queryList) { Console.WriteLine(“—> “ + item.Age + ” => Yaş Grubu Listesi”); foreach (string stud in item.Students) { Console.WriteLine(stud); } } //Output: //—> 20 => Yaş Grubu Listesi //Suresh Dasari //Praveen Alavala //Axel Sai //Brice Dustin //—> 35 => Yaş Grubu Listesi //Rohini Alavala //Sateesh Alavala //Adrian Sai //Alvin Dustin } class Student { public int Id { get; set; } public string Name { get; set; } public string Gender { get; set; } public int Age { get; set; } public List<string> Students { get; set; } } |
Başka bir örnek:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
List<Product> objProduct = new List<Product>() { new Product() {Name = “Tablet”, Count = 15,Price=20 }, new Product() {Name = “Keyboard”, Count = 10,Price=30 }, new Product() {Name = “Phone”, Count = 15,Price=50 }, new Product() {Name = “Mouse”, Count = 10,Price=100 }, new Product() {Name = “Notebook”, Count = 12,Price=123 }, new Product() {Name = “pencil”, Count = 12,Price=50 } }; var queryList = objProduct.Where(x => x.Price >= 50).GroupBy(x => new{ x.Price}) .Select(x => new Product { Price = x.Key.Price, Count = x.Sum(t => t.Count), Products = x.Select(a => a.Name).ToList() }); foreach (var item in queryList) { Console.WriteLine(“Fiyatı “ + item.Price + ” TL” + ” olan ürünler.”); Console.WriteLine(“Toplam “ + item.Count + ” Adet”); foreach (string pro in item.Products) { Console.WriteLine( “—>” + pro); } } //Output: //Fiyatı 50 TL olan ürünler. //Toplam 27 Adet //—>Phone //—>pencil //Fiyatı 100 TL olan ürünler. //Toplam 10 Adet //—>Mouse //Fiyatı 123 TL olan ürünler. //Toplam 12 Adet //—>Notebook class Product { public string Name { get; set; } public int Count { get; set; } public decimal Price { get; set; } public List<string> Products { get; set; } } |
sağlıcakla kalın…








Bir yanıt yazın