■ Enumerable 클래스의 Join<TOuter, TInner, TKey, TResult> 확장 메소드를 사용하는 방법을 보여준다.
▶ Program.cs
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
using System; using System.Collections.Generic; using System.Linq; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { // 강의 리스트를 설정한다. Course korean = new Course{ Title = "국어", HourCount = 3 }; Course english = new Course{ Title = "영어", HourCount = 3 }; Course mathematics = new Course{ Title = "수학", HourCount = 2 }; Course science = new Course{ Title = "과학", HourCount = 3 }; List<Course> courseList = new List<Course> { english, mathematics, science, korean }; // 학생 리스트를 설정한다. Student student1 = new Student { Name = "김철수", Course = science }; Student student2 = new Student { Name = "이영희", Course = korean }; Student student3 = new Student { Name = "홍길동", Course = english }; Student student4 = new Student { Name = "김지수", Course = science }; Student student5 = new Student { Name = "서지영", Course = english }; Student student6 = new Student { Name = "김지훈", Course = mathematics}; List<Student> studentList = new List<Student> { student1, student2, student3, student4, student5, student6 }; // 학생 리스트와 강의 리스트를 조인한다. var resultEnumerable = studentList.Join ( courseList, student => student.Course, course => course, (student, course) => new { StudentName = student.Name, CourseTitle = course.Title } ); // 결과를 출력한다. foreach(var result in resultEnumerable) { Console.WriteLine($"{result.StudentName} - {result.CourseTitle}"); } } #endregion } } |
▶ Course.cs
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 |
namespace TestProject { /// <summary> /// 강의 /// </summary> public class Course { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 강의 제목 - Title /// <summary> /// 강의 제목 /// </summary> public string Title { get; set; } #endregion #region 강의 시간 - HourCount /// <summary> /// 강의 시간 /// </summary> public int HourCount { get; set; } #endregion } } |
▶ Student.cs
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 |
namespace TestProject { /// <summary> /// 학생 /// </summary> public class Student { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 성명 - Name /// <summary> /// 성명 /// </summary> public string Name { get; set; } #endregion #region 강의 - Course /// <summary> /// 강의 /// </summary> public Course Course { get; set; } #endregion } } |