CODEKILLER

반응형

> 두 시퀀스를 연결하여 하나의 시퀀스를 반환

IEnumerable<TSource> 를 반환합니다.

Enumerable.Concat&lt;TSource&gt;
Enumerable.Concat<TSource>

public class Example
{
    public static void Main()
    {
        Console.Write("<< Enumerable.Concat 출력 예제 >> \n");

        Code[] codeOne = GetBeta();
        Code[] codeTwo = GetAlpha();

        IEnumerable<string> query =
            codeOne.Select(beta => beta.CodeName).Concat(codeTwo.Select(alpha => alpha.CodeName));

        foreach (string name in query)
        {
            Console.WriteLine(name);
        }

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

    static Code[] GetBeta()
    {
        Code[] betas = { new Code { CodeName= "betas1", CodeGrade=8 }, new Code { CodeName= "betas2", CodeGrade=4 }, new Code { CodeName= "betas3", CodeGrade=1 } };
        return betas;
    }

    static Code[] GetAlpha()
    {
        Code[] Alphas = { new Code { CodeName= "Alphas1", CodeGrade=3 }, new Code { CodeName= "Alphas2", CodeGrade=14 }, new Code { CodeName= "Alphas3", CodeGrade=9 } };
        return Alphas;
    }
}

class Code
{
    public string CodeName { get; set; }
    public int CodeGrade { get; set; }
}

 

> SelectMany를 이용하여 두 시퀀스 연결하기

public class Example
{
    public static void Main()
    {
        Console.Write("<< Enumerable.Concat 출력 예제 >> \n");

        Code[] codeOne = GetBeta();
        Code[] codeTwo = GetAlpha();

        IEnumerable<string> query =
            new[] { codeOne.Select(beta => beta.CodeName), codeTwo.Select(alpha => alpha.CodeName) }
            .SelectMany(name => name);

        foreach (string name in query)
        {
            Console.WriteLine(name);
        }

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

    static Code[] GetBeta()
    {
        Code[] betas = { new Code { CodeName= "betas1", CodeGrade=8 }, new Code { CodeName= "betas2", CodeGrade=4 }, new Code { CodeName= "betas3", CodeGrade=1 } };
        return betas;
    }

    static Code[] GetAlpha()
    {
        Code[] Alphas = { new Code { CodeName= "Alphas1", CodeGrade=3 }, new Code { CodeName= "Alphas2", CodeGrade=14 }, new Code { CodeName= "Alphas3", CodeGrade=9 } };
        return Alphas;
    }
}

class Code
{
    public string CodeName { get; set; }
    public int CodeGrade { get; set; }
}
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band