ABOUT ME

지식의 공유가 세상을 발전 시킨다.

Today
Yesterday
Total
  • LINQ-Group BY
    c# 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();


            }

        }

    }

    'c#' 카테고리의 다른 글

    데이터 타입  (0) 2017.05.06
    LINQ-조인  (0) 2017.05.06
    C# datagridView 옵션  (0) 2017.05.02
    File ReadAllLines  (0) 2017.04.15
    문장에서 단어 포함여부 확인  (0) 2017.04.15
Designed by Tistory.