[C# 기초강의] Ch 15. LINQ

728x90
https://www.youtube.com/watch?v=_bOgIWzUlLo&list=PLVsNizTWUw7GN8wPRhclbKuQa9aI9Cj2V&index=16

 

📌 LINQ

◾LIN의 개념

  • C# 언어에 통합된 데이터 질의 기능
  • 프로그래밍에서 많은 부분을 차지하는 데이터 작업의 효율을 크게 향상
  • 데이터 질의란 데이터 집합에서 원하는 데이터를 찾는 작업
  • 데이터 질의 기본 요소
  1. From : 어떤 데이터 집합에서
  2. Where : 어떤 조건으로
  3. Select : 어떤 항목을
var profiles = from profile in arrProfile
			   where profile.Height < 175
               order by profile.Height
               select profile;

foreach (var profile in profiles)
	Console.WriteLine("{0}, {1}", profile.Name, profile.Height);

 

◾ from

  • from <범위변수> in <데이터원본>
int[] numbers = { 1, 2, ... 10}l

var result = from n in numbers
             where n % 2 == 0
             orderby n
             select n;

foreach (int n in result)
	Console.WriteLine("짝수 : {0}", n);

 

◾ where

  • 찾고자 하는 데이터를 필터
  • 범위 변수가 충족해야하는 조건을 매개변수로 입력

 

◾ orderby

  • 필터링된 데이터를 정렬
  • ascending 오름차순(디폴트), descending 내림차순

 

◾ select

  • 최종 결과를 추출하는 쿼리식의 마침표 같은 존재
  • LINQ 질의 결과인 IEnumeravle< T > 객체의 매개변수 T는 selset문에 의해 결정

 

◾ groupby

  • 특정 기준으로 데이터를 분류
var listProfile = from profile in arrProfile
                  group profile by profile Height < 175 into g
                  select new { GroupKey = g.Key, Profiles = g } ;

 

◾ join

  • 두 데이터 원본 연결
  • 특정 필드가 일치하는 데이터끼리 연결
    내부조인 : 교집합, 일치하는 데이터만 연결하여 반환
    외부조인 : 한쪽 데이터 원본을 기준으로 삼은 상태에서 다른 데이터 원본과 결합하여 반환
728x90