示例#1
0
        private static void simple(Profile[] arrProfile)
        {
            var profiles1 = from profile in arrProfile
                            where profile.Height < 175
                            orderby profile.Height
                            select new
                            {
                                Name = profile.Name,
                                InchHeight = profile.Height * 0.393
                            };
            Console.WriteLine("linq 쿼리문");
            foreach (var profile in profiles1)
                Console.WriteLine("{0}, {1}", profile.Name, profile.InchHeight);

            Console.WriteLine();
            Console.WriteLine("람다식");
            var profiles2 = 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 profiles1)
                Console.WriteLine("{0}, {1}", profile.Name, profile.InchHeight);
            Console.WriteLine();
        }
示例#2
0
        private void group(Profile[] arrProfile)
        {
            var listProfile1 = from profile in arrProfile
                               orderby profile.Height
                              group profile by profile.Height into g
                              select new { GroupKey = g.Key, Count = g.Count()};

            Console.WriteLine("linq 쿼리문");
            foreach (var profile in listProfile1)
            {
                Console.WriteLine("키 : {0}, 인원수 : {1}", profile.GroupKey, profile.Count);
            }
            Console.WriteLine();

            var listProfile2 = arrProfile.OrderBy(profile => profile.Height).GroupBy(profile => profile.Height).Select(profile => new { GroupKey = profile.Key, Count = profile.Count() });
            Console.WriteLine("람다식");
            foreach (var profile in listProfile1)
            {
                Console.WriteLine("키 : {0}, 인원수 : {1}", profile.GroupKey, profile.Count);
            }
            Console.WriteLine();
        }