c#

LINQ-조인

봄산 2017. 5. 6. 11:29

내부조인 : 

-기준 데이터A, 비교 데이터  중, A와 B의 교집합

-기준데이터 A에 존재하지만 B에 존재하지 않은 데이터는 조인된 결과에서 누락 됩니다.


from a in A

join b in B on a.XXX equals b.YYYY



외부조인:



외부 조인은 기본데이터에 들어 있는 모든 데이터는 포함되어 있다.




using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace Join

{

    class Program

    {


        class Profile

        {

            public string Name { get; set; }

            public int Height { get; set; }

        }


        class Product

        {

            public string Title { get; set; }

            public string Star { get; set; }

        }




        static void Main(string[] args)

        {


            Profile[] arrProfile =

            {

                 new Profile() {Name="정우성",Height=180 },

                 new Profile() {Name="김태희",Height=158},

                 new Profile() {Name="고현정",Height=172 },

                 new Profile() {Name="이운세",Height=178},

                 new Profile() {Name="하아",Height=171 },

                 new Profile() {Name="정재형",Height=170}

            };


            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,

                     Work = product.Title,

                     Height = profile.Height



                 };



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


            foreach (var profile in listProfile)

            {


                Console.WriteLine("이름:{0}, 작품:{1}, 키:{2}cm", profile.Name, profile.Work, profile.Height);

            }


            listProfile =

                from profle in arrProfile

                join product in arrProduct on profle.Name equals product.Star into ps

                from product in ps.DefaultIfEmpty(new Product() { Title = "" })

                select new

                {

                    Name = profle.Name,

                    Work = product.Title,

                    Height = profle.Height


                };



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


            foreach (var profile in listProfile)

            {


                Console.WriteLine("이름:{0}, 작품:{1}, 키:{2}cm", profile.Name, profile.Work, profile.Height);

            }




            Console.ReadKey();

        }

    }

}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Join
{
    class Program
    {

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

        class Product
        {
            public string Title { get; set; }
            public string Star { get; set; }
        }



        static void Main(string[] args)
        {

            Profile[] arrProfile =
            {
                 new Profile() {Name="정우성",Height=180 },
                 new Profile() {Name="김태희",Height=158},
                 new Profile() {Name="고현정",Height=172 },
                 new Profile() {Name="이운세",Height=178},
                 new Profile() {Name="하아",Height=171 },
                 new Profile() {Name="정재형",Height=170}
            };

            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,
                     Work = product.Title,
                     Height = profile.Height


                 };


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

            foreach (var profile in listProfile)
            {

                Console.WriteLine("이름:{0}, 작품:{1}, 키:{2}cm", profile.Name, profile.Work, profile.Height);
            }

            listProfile =
                from profle in arrProfile
                join product in arrProduct on profle.Name equals product.Star into ps
                from product in ps.DefaultIfEmpty(new Product() { Title = "" })
                select new
                {
                    Name = profle.Name,
                    Work = product.Title,
                    Height = profle.Height

                };


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

            foreach (var profile in listProfile)
            {

                Console.WriteLine("이름:{0}, 작품:{1}, 키:{2}cm", profile.Name, profile.Work, profile.Height);
            }



            Console.ReadKey();
        }
    }
}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace Join

{

    class Program

    {


        class Profile

        {

            public string Name { get; set; }

            public int Height { get; set; }

        }


        class Product

        {

            public string Title { get; set; }

            public string Star { get; set; }

        }




        static void Main(string[] args)

        {


            Profile[] arrProfile =

            {

                 new Profile() {Name="정우성",Height=180 },

                 new Profile() {Name="김태희",Height=158},

                 new Profile() {Name="고현정",Height=172 },

                 new Profile() {Name="이운세",Height=178},

                 new Profile() {Name="하아",Height=171 },

                 new Profile() {Name="정재형",Height=170}

            };


            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,

                     Work = product.Title,

                     Height = profile.Height



                 };



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


            foreach (var profile in listProfile)

            {


                Console.WriteLine("이름:{0}, 작품:{1}, 키:{2}cm", profile.Name, profile.Work, profile.Height);

            }


            listProfile =

                from profle in arrProfile

                join product in arrProduct on profle.Name equals product.Star into ps

                from product in ps.DefaultIfEmpty(new Product() { Title = "" })

                select new

                {

                    Name = profle.Name,

                    Work = product.Title,

                    Height = profle.Height


                };



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


            foreach (var profile in listProfile)

            {


                Console.WriteLine("이름:{0}, 작품:{1}, 키:{2}cm", profile.Name, profile.Work, profile.Height);

            }




            Console.ReadKey();

        }

    }

}