LINQ

    class Category
    {
        public string Name { get; set; }
        public int ID { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // The Three Parts of a LINQ Query:
            //  1. Data source.
            int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

            // 2. Query creation.
            // numQuery is an IEnumerable<int>
            var numQuery =
                from num in numbers
                where (num % 2) == 0
                select num;

            // 3. Query execution.
            foreach (int num in numQuery)
            {
                Console.Write(“{0,1} “, num);
            }
            Console.WriteLine();

            // LIST
            List<Person> pList = new List<Person>();
            pList.Add(new Person(“둘리”, 1, “123-4568-9012”, “대한민국”));
            pList.Add(new Person(“희동이”, 3, “000-0000-0000”, “서울”));
            pList.Add(new Person(“고길동”, 2, “000-0000-0000”, “서울”));
            pList.Add(new Person(“도우너”, 5, “000-0000-0000”, “온따빠야별”));
            pList.Add(new Person(“또치”, 4, “000-0000-0000”, “미국”));
            pList.Insert(2, new Person(“마이콜”, 6, “000-0000-0000”, “서울”));

            // query syntax

            IEnumerable<Person> seoulQuery =
                from per in pList
                where per.Address == “서울”
                select per;

            foreach (var per in seoulQuery)
            {
                Console.WriteLine(per.Name + “, ” + per.ID);
            }
            Console.WriteLine();

            // query syntax
            IEnumerable<Person> personQuery =
                from p in pList
                where (p.ID < 2) || (p.Address == “서울”)
                orderby p.Name ascending
                select p;

            foreach (var p in personQuery)
            {
                Console.WriteLine(p.Name + “, ” + p.ID);
            }
            Console.WriteLine();

            // method syntax
            IEnumerable<Person> personQuery2 = pList.Where(p => (p.ID < 2) || (p.Address == “서울”)).OrderBy(p => p);
            foreach (var p in personQuery2)
            {
                Console.WriteLine(p.Name + “, ” + p.ID);
            }
            Console.WriteLine();

            // JOIN
            List<Category> categories = new List<Category>()
            {
                new Category() { Name=”서울”, ID=1 },
                new Category() { Name=”대한민국”, ID=2 },
                new Category() { Name=”미국”, ID=3 },
                new Category() { Name=”온따빠야별”, ID=4 },
            };

            var query = from p in pList
                        join cat in categories on p.Address equals cat.Name
                        select new
                        {
                            p.Name,
                            p.Address,
                            cat.ID
                        };
            Console.WriteLine(“Join Query”);
            foreach (var q in query)
            {
                Console.WriteLine(q.Name + “, ” + q.Address + “, ” + q.ID);
            }
            Console.WriteLine();
        }
    }

 

 

// 결과

0 2 4 6
희동이, 3
마이콜, 6
고길동, 2

고길동, 2
둘리, 1
마이콜, 6
희동이, 3

고길동, 2
둘리, 1
마이콜, 6
희동이, 3

Join Query
둘리, 대한민국, 2
희동이, 서울, 1
마이콜, 서울, 1
고길동, 서울, 1
도우너, 온따빠야별, 4
또치, 미국, 3