CODEKILLER

반응형

> 키가 같은지 여부에 따라 두 시퀀스의 요소를 연관시키고 결과를 그룹화.

Linq - Enumerable.GroupJoin 출력.
Linq - Enumerable.GroupJoin 출력.

public class Example
{
    public static void Main()
    {
        Console.Write("<< Linq - Enumerable.GroupJoin 출력 예제 >> \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.GroupJoin(keywords,
                                 person => person,
                                 kword => kword.Owner,
                                 (person, petCollection) =>
                                     new
                                     {
                                         OwnerName = person.Name,
                                         Keywords = petCollection.Select(k => k.Name)
                                     });

        foreach (var obj in query)
        {
            Console.WriteLine("{0}:", obj.OwnerName);
            foreach (string keyword in obj.Keywords)
            {
                Console.WriteLine("  {0}", 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