[C#] Linq group by

c# 2023. 1. 11. 14:05
728x90

분류

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

class Program
{
    static void Main(string[] args)
    {
        Profile[] arrProfile = 
        {
            new Profile(){Name="정우성", Height = 500,
            new Profile(){Name="붐붐" ,Height = 456},
            new Profile(){Name="차차" ,Height = 789},
            new Profile(){Name="치치" ,Height = 123},
            new Profile(){Name="라라라" ,Height = 852},
        };
        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($"- 175미만 : {Group.GroupKey}");
            foreach (var profile in Group.Profiles)
            {
                Console.WriteLine($">>> {profile.Name}, {profile.Height}");
            }
        }
    }
}

group A by B into C

A에는 from 절에서 뽑아낸 범위 변수를, B에는 분류 기준을, C에는 그룹 변수를 위치시킴

 

 

728x90

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

[C#] StreamReader  (0) 2023.01.13
[C#] StreamWriter  (0) 2023.01.13
디버그 프로필에 지정된 디버그 실행 파일이 없습니다  (0) 2023.01.11
[C#] Func 대리자 Action 대리자  (0) 2023.01.11
[C#] Linq  (0) 2023.01.11
Posted by 바르마스
,