CODEKILLER

반응형

> 일치하는 키를 기준으로 두 시퀀스의 요소를 연관

Linq - Queryable.Join 출력.
Linq - Queryable.Join 출력.

 

public class Example
{
    public static void Main()
    {
        Console.Write("<< Linq - Queryable.Join 출력 예제 >> \n");

        Code kim = new Code { Name = "Mr.Kim" };
        Code lee = new Code { Name = "Mr.Lee" };
        Code choi = new Code { Name = "Mrs.Choi" };

        // 예약어를 사용할때는 @ 붙여서 사용.
        Keyword @double = new Keyword { Name = "double", Owner = lee };
        Keyword @int = new Keyword { Name = "int", Owner = lee };
        Keyword @char = new Keyword { Name = "char", Owner = choi };
        Keyword @decimal = new Keyword { Name = "decimal", Owner = kim };
        Keyword test = new Keyword { Name = "test", Owner = kim };
        Keyword test2 = new Keyword { Name = "test2", Owner = choi };

        List<Code> code = new List<Code> { kim, lee, choi };
        List<Keyword> keywords = new List<Keyword> { @double, @int, @char, @decimal, test, test2 };

        // Code-Keyword 쌍의 목록을 생성합니다.
        var query = code.AsQueryable().Join(keywords,
                        person => person,
                        keyword => keyword.Owner,
                        (person, keyword) =>
                            new { OwnerName = person.Name, keyword = keyword.Name });

        foreach (var obj in query)
        {
            Console.WriteLine("{0} - {1}", obj.OwnerName, obj.keyword);
        }

        Console.WriteLine(System.Environment.NewLine);
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }
}

class Code
{
    public string Name { get; set; }
}

class Keyword
{
    public string Name { get; set; }
    public Code Owner { get; set; }
}
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band