[C# 문법] C# LINQ 4장 – Group By 사용하기


안녕하세요.

 

오늘은 C# Linq 문법 4번째 시간으로써, 앞에서 간단히 Linq를 사용하는 방법들에 대해서 설명 및 예시를 들어 주었는데요.

 

이제는 여기서 추가로 Group By 라는 구문을 사용하는 방법에 대해서 알려 드리려고 합니다.

 

Group 이라면 말 그대로 특정 그룹을 지정해서 데이터를 조회하겠다고 생각 하시면 되겠는데요.

 

예를 들면, 학생 그룹이 있고 같은 반 학생 별로 그룹을 지어서 데이터를 조회하는 예제 코드를 작성해 보도록 하겠습니다.


 

group by 구문 형태


-     group A by B into C


위의 형태를 이용하여 group by 구문 예제를 작성해 보도록 하겠습니다.

 

예제 코드


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Linq_Test

{

    class Program

    {

        static void Main(string[] args)

        {

            Student[] _studentArr =

            {

                new Student() { _Class = "A", Name = "학생1", Age = 20, Score = 45 },

                new Student() { _Class = "A", Name = "학생2", Age = 20, Score = 55 },

                new Student() { _Class = "B", Name = "학생3", Age = 20, Score = 65 },

                new Student() { _Class = "A", Name = "학생4", Age = 20, Score = 25 },

                new Student() { _Class = "C", Name = "학생5", Age = 20, Score = 35 },

                new Student() { _Class = "D", Name = "학생6", Age = 20, Score = 15 },

                new Student() { _Class = "D", Name = "학생7", Age = 20, Score = 85 },

                new Student() { _Class = "B", Name = "학생8", Age = 20, Score = 99 },

                new Student() { _Class = "C", Name = "학생9", Age = 20, Score = 100 },

                new Student() { _Class = "C", Name = "학생10", Age = 20, Score = 51 }

            };

 

            //Linq 구문을 통해서 반별로 그룹 지어서 조회하기

            var query = from arr in _studentArr

                        orderby arr._Class, arr.Score ascending

                        group arr by arr._Class into g

                        select new

                        {

                            GroupKey = g.Key,

                            Student = g

                        };

 

            //출력하기

            foreach (var Group in query)

            {

                Console.WriteLine($"반별로 그룹 : {Group.GroupKey}");

 

                foreach (var stduetns in Group.Student)

                {

                    Console.WriteLine($"{stduetns.Name}, {stduetns.Score}");

                }

 

                Console.WriteLine();

                Console.WriteLine();

            }

        }      

    }

 

    /// <summary>

    /// 학생 클래스 선언

    /// </summary>

    public class Student

    {

        public string _Class { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public int Score { get; set; }

    }

}

 

Colored by Color Scripter

cs

 

실행 결과



위와 같이 반별로 그룹 지어서 알맞게 조회된 것을 확인하실 수 있습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY