c#

LINQ-Group BY

봄산 2017. 5. 6. 10:38

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace GroupBy

{


    //group A by B into C

    //A는 from절에서 뽑아낸 변수 B는 분류 기준 C는 그룹변수

    //Igrop


    class Program

    {

        class Profile

        {

            public string Name { get; set; }

            public int Height { get; set; }

        }


        static void Main(string[] args)

        {


            Profile[] arrProfile =

            {

                 new Profile() {Name="정우성",Height=180 },

                 new Profile() {Name="김태희",Height=158},

                 new Profile() {Name="고현정",Height=172 },

                 new Profile() {Name="이운세",Height=178},

                 new Profile() {Name="하아",Height=171 },

                 new Profile() {Name="정재형",Height=170},

            };



            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);


                }



            }


            Console.ReadKey();


        }

    }

}