CODEKILLER

반응형

C# Linq Join 문
C# Linq Join 문

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

        List<Code> codes = new List<Code>
        {
            new Code { Name = "CodeA", CategoryId = 0 },
            new Code { Name = "CodeB", CategoryId = 0 },
            new Code { Name = "CodeC", CategoryId = 1 },
            new Code { Name = "CodeD", CategoryId = 1 },
            new Code { Name = "CodeE", CategoryId = 2 },
        };

        List<KeyCategory> keyCategories = new List<KeyCategory>
        {
            new KeyCategory { Id = 0, CategoryName = "CodeZero" },
            new KeyCategory { Id = 1, CategoryName = "CodeOne" },
            new KeyCategory { Id = 2, CategoryName = "CodeTwo" }
        };

        var query = from code in codes
                    join category in keyCategories on code.CategoryId equals category.Id
                    select new { code.Name, category.CategoryName };

        foreach (var item in query)
        {
            Console.WriteLine($"{item.Name} - {item.CategoryName}");
        }


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

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

class KeyCategory
{
    public int Id { get; set; }
    public string CategoryName { get; set; }
}
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band