오늘의 C# 공부 : LinQ

////LinQ
////링크

//using System;
//using System.Linq;

//namespace From
//{
//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            int[] numbers = { 9, 2, 6, 4, 8, 3, 7, 8, 1, 10 };

//            var result = from n in numbers
//                         where n % 2 == 0
//                         orderby n
//                         select n;

//            foreach (int n in result)
//            //for(int n=0;n<result.;n++)
//                Console.WriteLine(“짝수 : {0}”, n);
//        }
//    }
//}

//using System;
//using System.Linq;

//namespace SimpleLinq
//{
//    class Profile
//    {
//        public string Name { get; set; }
//        public int Height { get; set; }
//    }

//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Profile[] arrProfile =
//            {
//                new Profile(){Name = “정우성”, Height = 186},
//                new Profile(){Name = “김태희”, Height = 158},
//                new Profile(){Name = “고현정”, Height = 178},
//                new Profile(){Name = “이문세”, Height = 178},
//                new Profile(){Name = “하하”, Height = 171}
//            };

//            var profiles = from profile in arrProfile
//                           where profile.Height > 175
//                           orderby profile.Height
//                           select new
//                           {
//                               Name = profile.Name,
//                               InchHeight = profile.Height * 0.393
//                           };

//            foreach (var profile in profiles)
//                Console.WriteLine("{0},{1}", profile.Name, profile.InchHeight);
//        }
//    }
//}

////여러 개의 테이터 원본에 질의하기
//using System;
//using System.Linq;

//namespace FormFrom
//{
//    class Class
//    {
//        public string Name { get; set; }
//        public int[] Score { get; set; }
//    }

//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Class[] arrClass =
//            {
//                new Class() { Name = “연두반” , Score = new int[] {99,80,70,24}},
//                new Class() { Name = “분홍반” , Score = new int[] {60,45,87,72}},
//                new Class() { Name = “파랑반” , Score = new int[] {92,30,85,94}},
//                new Class() { Name = “노랑반” , Score = new int[] {90,88,0,17}}
//            };

//            var classes = from c in arrClass
//                            from s in c.Score
//                            where s < 60
//                            orderby s
//                          select new { c.Name, Lowest = s };

//            foreach (var c in classes)
//                Console.WriteLine(“낙제 : {0}  {1}",c.Name ,c.Lowest);
//        }
//    }
//}

////group by로 데이터 분류하기

//using System;
//using System.Linq;

//namespace GroupBy
//{
//    class Profile
//    {
//        public string Name { get; set; }
//        public int Height { get; set; }
//    }

//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Profile[] arrProfile =
//            {
//                new Profile(){Name = “정우성” , Height = 186},
//                new Profile(){Name = “김태희” , Height = 158},
//                new Profile(){Name = “고현정” , Height = 172},
//                new Profile(){Name = “이문세” , Height = 178},
//                new Profile(){Name = “하하” , Height = 171}
//            };

//            var listProfile = from profile in arrProfile
//                              orderby profile.Height
//                              group profile by profile.Height < 175 into g
//                              select new { GroupKey = g.Key, profiles = g };

//            foreach (var Group in listProfile)
//            {
//                Console.WriteLine ( “-175cm 미만? : {0}",Group.GroupKey);

//                foreach (var profile in Group.profiles)
//                {
//                    Console.WriteLine(”     {0},{1}”, profile.Name, profile.Height);
//                }
//            }
//        }
//    }
//}

////join
//using System;
//using System.Linq;

//namespace Join
//{
//    class Profile
//    {
//        public string Name { get; set; }
//        public int Height { get; set; }
//    }

//    class Product
//    {
//        public string Title { get; set; }
//        public string Star { get; set; }
//    }

//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Profile[] arrProfile =
//            {
//                new Profile() { Name = “정우성” , Height = 186},
//                new Profile() { Name = “김태희” , Height = 158},
//                new Profile() { Name = “고현정” , Height = 172},
//                new Profile() { Name = “이문세” , Height = 178},
//                new Profile() { Name = “하하” , Height = 171}
//            };

//            Product[] arrProduct =
//            {
//                new Product() { Title = “비트” , Star = “정우성”},
//                new Product() { Title = “CF 다수” , Star = “김태희”},
//                new Product() { Title = “아이리스” , Star = “김태희”},
//                new Product() { Title = “모래시계”, Star = “고현정”},
//                new Product() { Title = “솔로예찬”, Star = “이문세”}
//            };

//            var listProfile =
//                from profile in arrProfile
//                join product in arrProduct on profile.Name equals product.Star
//                select new
//                {
//                    Name = profile.Name,
//                    Work = product.Title,
//                    Height = profile.Height
//                };

//            Console.WriteLine("—–내부 조인 결과 ———-");
//            foreach (var profile in listProfile)
//            {
//                Console.WriteLine(“이름: {0} , 작품:{1} , 키: {2}”, profile.Name, profile.Work, profile.Height);
//            }

//            listProfile =
//                from profile in arrProfile
//                join product in arrProduct on profile.Name equals product.Star into ps //두 개를 조인해서 ps에 집어 넣는다.
//                from product in ps.DefaultIfEmpty(new Product() { Title = “그런거 없음” })
//                select new
//                {
//                    Name = profile.Name,
//                    Work = product.Title,
//                    Height = profile.Height
//                };

//            Console.WriteLine();

//            Console.WriteLine("————-외부 조인 결과——————–");
//            foreach (var profile in listProfile)
//            {
//                Console.WriteLine(“이름:{0},작품{1},키{2}cm”, profile.Name, profile.Work, profile.Height);
//            }

//        }
//    }
//}

////심플Linq

//using System;
//using System.Linq;

//namespace SimpleLinq2
//{
//    class Profile
//    {
//        public string Name { get; set; }
//        public int Height { get; set; }
//    }

//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Profile[] arrProfile =
//            {
//                new Profile() { Name = “정우성”, Height = 186},
//                new Profile() { Name = “김태희”, Height = 158},
//                new Profile() { Name = “고현정”, Height = 172},
//                new Profile() { Name = “이문세”, Height = 178},
//                new Profile() { Name = “하하” , Height = 171}
//            };

//            var profiles = arrProfile.
//                Where(profile => profile.Height < 175).
//                OrderBy(profile => profile.Height).
//                Select(profile =>
//                    new
//                    {
//                        Name = profile.Name,
//                        InchHeight = profile.Height * 0.393
//                    });

//            foreach (var profile in profiles)
//                Console.WriteLine("{0},{1}", profile.Name, profile.InchHeight);
//        }
//    }
//}

//

using System;
using System.Linq;

namespace MinMaxAvg
{
    class Profile
    {
        public string Name { get; set; }
        public int Height { get; set; }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Profile[] arrProfile =
           {
               new Profile() {Name = “정우성” , Height = 186},
               new Profile() {Name = “김태희” , Height = 158},
               new Profile() { Name = “고현정” , Height = 172},
               new Profile() { Name = “이문세”, Height = 178},
               new Profile() { Name = “하하”, Height = 171}
           };

            var heightStat = from profile in arrProfile
                             group profile by profile.Height < 175 into g
                             select new
                             {
                                 Group = g.Key == true? “175미만”:“175이상”,
                                 Count = g.Count(),
                                 Max   = g.Max(profile => profile.Height),
                                 Min   = g.Min(profile => profile.Height),
                                 Average = g.Average(profile =>profile.Height)
                             };

            foreach (var stat in heightStat)
            {
                Console.Write("{0} - Count :{1}, Max: {2}, “,
                    stat.Group, stat.Count, stat.Max);
                Console.WriteLine(“Min : {0} , Average : {1}”, stat.Min, stat.Average);
            }
        }
    }
}

Hugo로 만듦
JimmyStack 테마 사용 중