Development/C#
LINQ Sample
@위너스
2013. 1. 31. 14:54
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace cInitialize { class Country { public string Name { get; set; } public int Area { get; set; } public int Population { get; set; } } class EtcInfo { public string Name { get; set; } public string Etc { get; set; } } class cQuery2 { static public void CallMethod() { ListlistCon = new List { new Country{Name="한국",Area=1000000,Population=5000}, new Country{Name="일본",Area=500000,Population=3000}, new Country{Name="중국",Area=2000000,Population=50000}, new Country{Name="대만",Area=800000,Population=4000}, new Country{Name="한국",Area=1500000,Population=5500}, new Country{Name="대만",Area=800000,Population=4000} }; List listEtc = new List { new EtcInfo{Name="한국",Etc="아시아1"}, new EtcInfo{Name="일본",Etc="아시아2"}, new EtcInfo{Name="중국",Etc="아시아3"}, new EtcInfo{Name="대만",Etc="아시아4"} }; /* 1. LINQ where절 */ IEnumerable coutryAreaQuery = from country in listCon where country.Area > 500000 select country; foreach (var item in coutryAreaQuery) { Console.WriteLine("{0}:면적<{1}>, 인구<{2}>", item.Name, item.Area, item.Population); } /* 2. LINQ group */ Console.WriteLine("====================================="); var queryContryGroups = from c in listCon group c by c.Name; foreach (var item in queryContryGroups) { Console.WriteLine("----{0} 그룹----", item.Key); foreach (var item2 in item) { Console.WriteLine("{0}:면적<{1}>, 인구<{2}>", item2.Name, item2.Area, item2.Population); } } Console.WriteLine(); /* 3. LINQ where절2 */ IEnumerable queryCountryPop = from c in listCon where c.Population > 30000 && c.Population < 60000 select c; foreach (var item in queryCountryPop) { Console.WriteLine("{0}:면적<{1}>, 인구<{2}>", item.Name, item.Area, item.Population); } Console.WriteLine(); /* 5. LINQ orderby절 */ IEnumerable querySortedCountries = from c in listCon orderby c.Area ascending, c.Name descending select c; foreach (var item in querySortedCountries) { Console.WriteLine("{0}:면적<{1}>, 인구<{2}>", item.Name, item.Area, item.Population); } Console.WriteLine(); /* 6. LINQ join절 */ var categoryQuery = from c in listCon join e in listEtc on c.Name equals e.Name select new { Name = c.Name, Area = c.Area, Population = c.Population, Etc = e.Etc }; foreach (var item in categoryQuery) { Console.WriteLine("{0}:면적<{1}>, 인구<{2}>, 분류<{3}>", item.Name, item.Area, item.Population, item.Etc); } Console.WriteLine(); /* 7. LINQ let절*/ string[] PhoneNum = { "010-333-5555", "010-243-5434", "011-3243-2533" }; IEnumerable queryFirstNum = from p in PhoneNum let firstNum = p.Split(new char[] { '-' })[0] select firstNum; foreach (var item in queryFirstNum) { Console.WriteLine(item); } Console.WriteLine(); string[] PhoneNum2 = { "010-333-5555", "010-243-5434", "011-3243-2533" }; var queryFirstNum2 = from p in PhoneNum2 select new { Num1 = p.Split('-')[0], Num2 = p.Split('-')[1], Num3 = p.Split('-')[2] }; foreach (var item in queryFirstNum2) { Console.WriteLine("{0}){1}_{2}", item.Num1, item.Num2, item.Num3); } Console.WriteLine(); } } }