C#프로그래밍 21 : LINQ

LeeWonjin·2022년 6월 9일
0

[학부]C#프로그래밍

목록 보기
21/21

LINQ 개요

Language INtegrated Query
데이터 질의기능

최종질의결과로 IEnumerable<\T> 반환
컬렉션을 반환한다고 볼 수 있음.

front where orderby select

  • from : 범위변수 선언, 원본데이터 선택
  • where : 검색 조건
  • orderby
    : 정렬기준 정렬방향(ascending/descending)
    ** 정렬방향의 기본값은 ascending
  • select : 질의결과 추출. 질의결과 형식매개변수 T 결정.
using System;
using System.Linq;

namespace Program
{
    class Gorani
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Gorani[] goranis =
            {
                new Gorani() {Name="a", Age=5},
                new Gorani() {Name="b", Age=1},
                new Gorani() {Name="c", Age=2},
                new Gorani() {Name="d", Age=4},
                new Gorani() {Name="e", Age=3},
            };

            var result_1 = from gorani in goranis
                           where gorani.Age > 2
                           orderby gorani.Age descending
                           select gorani;
            Console.WriteLine("===result_1===");
            foreach (var gorani in result_1)
                Console.WriteLine($"{gorani.Name} {gorani.Age}");
            Console.WriteLine();

            var result_2 = from gorani in goranis
                           where gorani.Age < 4
                           orderby gorani.Name
                           select gorani.Name;
            Console.WriteLine("===result_2===");
            foreach (var name in result_2)
                Console.WriteLine($"{name}");
            Console.WriteLine();

            var result_3 = from gorani in goranis
                           where gorani.Age > 0
                           orderby gorani.Name descending
                           select new { ItsName = gorani.Name, Age=gorani.Age };
            Console.WriteLine("===result_3===");
            foreach (var gorani in result_3)
                Console.WriteLine($"{gorani.ItsName} {gorani.Age}");
            Console.WriteLine();
            /*
            ===result_1===
            a 5
            d 4
            e 3

            ===result_2===
            b
            c
            e

            ===result_3===
            e 3
            d 4
            c 2
            b 1
            a 5
             */
        }
    }
}

groupby

using System;
using System.Linq;

namespace Program
{
    class Gorani
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Gorani[] goranis =
            {
                new Gorani() {Name="a", Age=5},
                new Gorani() {Name="b", Age=1},
                new Gorani() {Name="c", Age=2},
                new Gorani() {Name="d", Age=4},
                new Gorani() {Name="e", Age=3},
            };

            var result = from gorani in goranis
                         group gorani by gorani.Age < 3 into g
                         select new { Group = g.Key, Goranis = g };
            foreach(var group in result)
            {
                Console.WriteLine($"----- {group} -----");
                foreach(var gorani in group.Goranis)
                    Console.WriteLine($"{gorani.Name} {gorani.Age}");
            }

            /*
            ----- { Group = False, Goranis = System.Linq.Grouping`2[System.Boolean,Program.Gorani] } -----
            a 5
            d 4
            e 3
            ----- { Group = True, Goranis = System.Linq.Grouping`2[System.Boolean,Program.Gorani] } -----
            b 1
            c 2
             */
        }
    }
}

join

내부조인

두 데이터에서 일치하는 부분을 연결

using System;
using System.Linq;

namespace Program
{
    class Gorani // 고라니
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    class Owner // 고라니 오-너
    {
        public string Name { get; set; }
        public string GoraniName { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Gorani[] goranis =
            {
                new Gorani() {Name="a", Age=5},
                new Gorani() {Name="b", Age=1},
                new Gorani() {Name="c", Age=2},
                new Gorani() {Name="d", Age=4},
                new Gorani() {Name="e", Age=3},
            };

            Owner[] owners =
            {
                new Owner() {Name="X", GoraniName="a"},
                new Owner() {Name="Y", GoraniName="c"},
                new Owner() {Name="Y", GoraniName="d"},
                new Owner() {Name="Z", GoraniName="e"},
            };

            var result = from gorani in goranis
                         join owner in owners
                         on gorani.Name equals owner.GoraniName
                         select new { PersonName = owner.Name, GoraniName = gorani.Name };

            foreach(var item in result)
            {
                Console.WriteLine($"{item.PersonName} {item.GoraniName}");
            }

            /*
            X a
            Y c
            Y d
            Z e
             */
        }
    }
}

내부조인

한 데이터를 기준으로 다른 데이터 원본과 연결

using System;
using System.Linq;

namespace Program
{
    class Gorani // 고라니
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    class Owner // 고라니 오-너
    {
        public string Name { get; set; }
        public string GoraniName { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Gorani[] goranis =
            {
                new Gorani() {Name="a", Age=5},
                new Gorani() {Name="b", Age=1},
                new Gorani() {Name="c", Age=2},
                new Gorani() {Name="d", Age=4},
                new Gorani() {Name="e", Age=3},
            };

            Owner[] owners =
            {
                new Owner() {Name="X", GoraniName="a"},
                new Owner() {Name="Y", GoraniName="c"},
                new Owner() {Name="Y", GoraniName="d"},
                new Owner() {Name="Z", GoraniName="e"},
            };

            var result = from gorani in goranis
                         join owner in owners
                         on gorani.Name equals owner.GoraniName into ps
                         from temp_owner in ps.DefaultIfEmpty(new Owner() { Name="None" })
                         select new { PersonName = temp_owner.Name, GoraniName = gorani.Name };

            foreach(var item in result)
                Console.WriteLine($"{item.PersonName} {item.GoraniName}");

            /*
            X a
            None b
            Y c
            Y d
            Z e
             */
        }
    }
}
profile
노는게 제일 좋습니다.

0개의 댓글