[C#닷넷링크강좌]Linq,외부조인(Outer Join),쿼리식을메소드기반쿼리식으로변환예제
					
						꽁스짱					
																
							
							
							C#						
										
					
					0					
					
					2644
															
						
						
							2021.02.15 23:17						
					
				
			[C#닷넷링크강좌]Linq,외부조인(Outer Join),쿼리식을메소드기반쿼리식으로변환예제
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
class Customer
{
    public string Name { get; set; }
    public int Age { get; set; }
}
class Sale
{
    public string Name { get; set; }
    public string Goods { get; set; }
}
class Onj
{
    static void Main(string[] args)
    {
        Customer[] customer = {
                                new Customer() {Name="ONJSYSTEM", Age=8},
                                new Customer() {Name="오라클자바커뮤니티실무학원", Age=6},
                                new Customer() {Name="오라클자바커뮤니티", Age=13}
                                };
        Sale[] sale = {
                        new Sale() {Name="ONJSYSTEM", Goods="볼펜"},
                        new Sale() {Name="오라클자바커뮤니티실무학원", Goods="연필"},
                        };
        //쿼리식 기반
        var result1 = from c in customer
                      join s in sale on c.Name equals s.Name into tmp
                      from s in tmp.DefaultIfEmpty(new Sale() { Goods = "상품없음" })
                      select new
                      {
                          Name = c.Name,
                          Age = c.Age,
                          Goods = s.Goods
                      };
        foreach (var x in result1)
        {
            Console.WriteLine("이름 : {0}, 나이 : {1}, 상품 : {2}", x.Name, x.Age, x.Goods);
        }
        //메소드 기반 쿼리식으로
        var result2 = customer.GroupJoin(
                                    sale,
                                    c => c.Name,
                                    s => s.Name,
                                    (c1, s1) => new { c = c1, sale = s1 }
                                    )
                                    .SelectMany(
                                        custsale => custsale.sale.DefaultIfEmpty(new Sale() { Goods="상품없음"}),
                                        (x, y) => new { c = x.c, s = y }
                                     );
        Console.WriteLine("-----------------------------------------------------");
        foreach (var x in result2)
        {
            Console.WriteLine("이름 : {0}, 나이 : {1}, 상품 : {2}", x.c.Name, x.c.Age, x.s.Goods);
        }
    }
}
/*
 [결과] 
 이름 : ONJSYSTEM, 나이 : 8, 상품 : 볼펜
이름 : 오라클자바커뮤니티실무학원, 나이 : 6, 상품 : 연필
이름 : 오라클자바커뮤니티, 나이 : 13, 상품 : 상품없음
-----------------------------------------------------
이름 : ONJSYSTEM, 나이 : 8, 상품 : 볼펜
이름 : 오라클자바커뮤니티실무학원, 나이 : 6, 상품 : 연필
이름 : 오라클자바커뮤니티, 나이 : 13, 상품 : 상품없음
 * */

 
															
