모름

내부 조인

 

내부 조인(innerJoin)은 교집합과 비슷합니다. 두 데이터 원본 사이에서 특정 필드를 비교하여 일치하는 데이터를 모아 반환합니다. 내부 조인은 join 절을 통해 수행할 수 있습니다.

 

from a in A
join b in B on a.XXXX equals b.YYYY

 

위에서 보듯, from절을 통해 a라는 범위 변수를 뽑아냅니다. 이를 가지고 join절에서 b라는 데이터를 뽑아낼 수 있습니다.  이때 on 절에는 조건이 들어가는데 여기선  equals라는 "동등" 이라는 조건만 사용됩니다. 크고 작은 비교연산은 불가합니다.

 

 

 


 

 

 

외부 조인

 

외부 조인(outer joun)은 기본적으로 내부 조인과 비슷합니다. 하지만 외부 조인은 기준이 되는 데이터 원본이 포함됩니다. (내부조인은 동등한 조건에 맞는 데이터만 반환한다는 점에서 차이가 있습니다)

 

이해가 어려울땐 역시 직접 예제 프로그램을 작성해보는게 좋습니다. 그럼...

 

 

 


 

 

 

연습

 

using System;
using System.Linq;
using System.Collections.Generic;

namespace LinqJoin
{
    class Profile
    {
        public string Name { get; set; }
        public int    Height { get; set; }
    }

    class Product
    {
        public string Title { get; set; }
        public string Star { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Profile[] arrProfile =
            {
                new Profile(){Name = "정우성" , Height = 186},
                new Profile(){Name = "김태희" , Height = 158},
                new Profile(){Name = "고현정" , Height = 172},
                new Profile(){Name = "이문세" , Height = 178},
                new Profile(){Name = "하하" , Height = 171},
            };

            Product[] arrProduct =
            {
                new Product(){Title = "비트", Star ="정우성"},
                new Product(){Title = "CF 다수", Star ="김태희"},
                new Product(){Title = "아이리스", Star ="김태희"},
                new Product(){Title = "모래시계", Star ="고현정"},
                new Product(){Title = "솔로예찬", Star ="이문세"},
            };

            //내부조인
            var listProfile = from profile in arrProfile
                              join product in arrProduct on profile.Name equals product.Star
                              select new
                              {
                                  Name = profile.Name,
                                  Product = product.Title,
                                  Height = profile.Height
                              };

            Console.WriteLine("--- 내부 조인 결과 ---");

            foreach (var profile in listProfile)
            {
                Console.WriteLine($"이름 : {profile.Name}, 작품 : {profile.Product}, 키 : {profile.Height}");
            }

            //외부조인
            var listProfile2 = from profile in arrProfile
                               join product in arrProduct on profile.Name equals product.Star into ps
                               from product in ps.DefaultIfEmpty(new Product() { Title = "대표작 없음"})
                               select new
                               {
                                   Name = profile.Name,
                                   Work = product.Title,
                                   Height = profile.Height
                               };

            Console.WriteLine("--- 외부 조인 결과 ---");

            foreach (var profile in listProfile2)
            {
                Console.WriteLine($"이름 : {profile.Name}, 작품 : {profile.Work}, 키 : {profile.Height}");
            }
        }
    }
}

 

 

 


 

 

 

출력

 

 

외부 조인은 기본 데이터에서 교집합이 되지 않는 부분을 포함시키고 있는것이 보입니다. (하하)