![]()
Linq ile datalarımızı çekerken Listeleri karşılaştırma , sorgu sonuçlarını birleştirme , Listeyi tekrarsız getirme gibi işlemler yapıyoruz. Linq da bu işlemleri Union() , Distinct() , Except() ,Concat() methodlarını kullanarak yapıyoruz. Şimdi tek tek bunları inceleyelim.
Union() : Farklı iki sorgu sonucunu birleştirmek için kullanılır. Birleştirme sonrasında metod geriye tek bir sonuç dönderir. Bu işlemi yaparken tekrarlı kayıtları bir kere ekler. Özellikle join işlemlerinde sıklıkla kullanılır.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
string[] count1 = { “UK”, “Australia”, “India”, “USA” }; string[] count2 = { “India”, “Canada”, “UK”, “China” }; var result = count1.Union(count2); foreach (var item in result) { Console.Write(“{0}, “ , item); } //Output: UK, Australia, India, USA, Canada, China, |
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 |
List<Employee> Employee = new List<Employee>() { new Employee { Name = “Madhav”, DeptId=1 }, new Employee { Name = “Rohini”, DeptId=1 }, new Employee { Name = “Madhav”, DeptId=4 }, new Employee { Name = “Sateesh”, DeptId =4}, new Employee { Name = “Madhav”,DeptId = 1} }; List<Department> Department = new List<Department>() { new Department{DepId=1,DepName=“Finance”,Price = 500}, new Department{DepId=2,DepName=“Finance”,Price = 1000}, new Department{DepId=3,DepName=“Software”,Price = 1500}, new Department{DepId=4,DepName=“Software”,Price = 2000}, }; var result = ((from e in Employee where e.Name == “Madhav” select e.Name) .Union(from d in Department where (d.DepId == 1 || d.DepId == 4) && d.Price >= 500 select d.DepName)).ToList(); foreach (var item in result) { Console.Write(“{0}, “, item); } //Output: Madhav, Finance, Software, class Department { public int DepId { get; set; } public string DepName { get; set; } public decimal Price { get; set; } } class Employee { public int EmpId { get; set; } public string Name { get; set; } public int DeptId { get; set; } } |
Distinct() : Çektiğimiz listedeki kayıtları tekrarsız getirir.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
string[] countries = { “UK”, “uk”, “Australia”, “INDIA”, “india”, “USA” } var result = countries.Distinct(StringComparer.OrdinalIgnoreCase); foreach (var item in result) { Console.Write(“{0}, “, item); } //Output: UK, Australia, INDIA, USA, |
Except() : Birinci listede olupta ikinci listede olmayan verileir getirir. İki listede de varsa yine getirmez.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int[] list1 = { 0, 2, 4, 5, 6, 8, 9, 10 }; int[] list2 = { 1, 3, 5, 7, 8 ,10 }; var exeptList = list1.Except(list2); foreach (var item in exeptList) { Console.Write(“{0}, “, item); } //Output: 0, 2, 4, 6, 9, |
Başka bir örnek
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
string[] liste1 = { “Ahmet”, “Mustafa”, “Hakan”, “Seda”, “Hülya”,“Mert” }; string[] liste2 = { “Adem”, “Seda”, “Hakan”, “Ayşe”,“Nuran”,“Kemal” }; var result = liste1.Except(liste2); foreach (var item in result) { Console.Write(“{0}, “, item); } //Output: Ahmet, Mustafa, Hülya, Mert, |
Concat() : İki farklı listeyi birleştirmek için kullanılır. İki listede bulunan tüm elemanları tekrar etsede getirir.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
int[] list1 = { 0, 2, 4, 5, 6, 8, 9, 10 }; int[] list2 = { 1, 3, 5, 7, 8, 10 }; var exeptList = list1.Concat(list2); foreach (var item in exeptList) { Console.Write(“{0}, “, item); } //Output: 0, 2, 4, 5, 6, 8, 9, 10, 1, 3, 5, 7, 8, 10, |
sağlıcakla kalın….








Bir yanıt yazın