■ Enumerable 클래스의 ToLookup<TSource, TKey, TElement> 확장 메소드를 사용해 데이터를 분류하는 방법을 보여준다.
▶ 예제 코드 (C#)
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 56 |
List<Company> companyList = new List<Company> { new Company { Name = "회사1", Category = "제조" , Address = "수원" }, new Company { Name = "회사2", Category = "서비스", Address = "서울" }, new Company { Name = "회사3", Category = "제조" , Address = "대전" }, new Company { Name = "회사4", Category = "제조" , Address = "대전" }, new Company { Name = "회사4", Category = "서비스", Address = "부산" } }; ILookup<string, string> lookup = companyList.ToLookup(company => company.Category, company => $"{company.Name} : {company.Address}"); foreach(IGrouping<string, string> grouping in lookup) { Console.WriteLine(grouping.Key); foreach(string item in grouping) { Console.WriteLine($" {item}"); } } /// <summary> /// 회사 /// </summary> public class Company { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { get; set; } #endregion #region 분류 - Category /// <summary> /// 분류 /// </summary> public string Category { get; set; } #endregion #region 주소 - Address /// <summary> /// 주소 /// </summary> public string Address { get; set; } #endregion } |