■ Join 확장 메소드 사용하기

------------------------------------------------------------------------------------------------------------------------

Program.cs

 

 

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

 

 

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

 

 

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

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요