■ Queryable 클래스의 GroupJoin<TOuter, TInner, TKey, TResult> 확장 메소드를 사용하는 방법을 보여준다.
▶ School.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
namespace TestProject { /// <summary> /// 학교 /// </summary> public class School { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { 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 학교 - School /// <summary> /// 학교 /// </summary> public School School { get; set; } #endregion } } |
▶ 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { School school1 = new School { Name = "학교1" }; School school2 = new School { Name = "학교2" }; School school3 = new School { Name = "학교3" }; Student student1 = new Student { Name = "학생1", School = school2 }; Student student2 = new Student { Name = "학생2", School = school2 }; Student student3 = new Student { Name = "학생3", School = school3 }; Student student4 = new Student { Name = "학생4", School = school1 }; Student student5 = new Student { Name = "학생5", School = school1 }; Student student6 = new Student { Name = "학생6", School = school3 }; List<School> schoolList = new List<School> { school1, school2, school3 }; List<Student> studentList = new List<Student> { student1, student2, student3, student4, student5, student6 }; var resultQueryable = schoolList .AsQueryable() .GroupJoin ( studentList, school => school, student => student.School, (school, sutdentCollection) => new { SchoolName = school.Name, studentNameEnumerable = sutdentCollection.Select(student => student.Name) } ); foreach(var result in resultQueryable) { Console.WriteLine(result.SchoolName); foreach(string studentName in result.studentNameEnumerable) { Console.WriteLine($" {studentName}"); } } /* 학교1 학생4 학생5 학교2 학생1 학생2 학교3 학생3 학생6 */ Console.ReadKey(false); } #endregion } |