본문 바로가기

C#/복습

2020-05-25 LINQ의 기본

@LINQ(Language-Integrated Query)

 

-쿼리 기능

 

-from: 어디에서 찾을 것인지

        └from 범위 변수 in 데이터 원본

-where: 조건이 무엇인지

        └where 조건식

-select: 어떤 것을 가져올 것인지

        └select 범위 변수

 

       

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Repeat_021
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] data = new int[] { 05498102332 };
 
            var QueryData = //IEnumerable<int> QueryData =
                from temp in data
                where temp < 100 // if(temp < 100)
                select temp;
 
            var test = from temp in data
                       select temp;
 
            foreach (int n in QueryData)
            {
                Console.WriteLine("QueryData: " + n);
            }
 
            //람다식 사용과 비교..
 
            List<int> listData = new List<int>(data);
            List<int> findAllData = listData.FindAll(a => a < 100);
 
            foreach (int n in findAllData) {
                Console.WriteLine("findAllData " + n);
            }
        }
    }
}
 
 
 

 

응용예제

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Repeat_021_1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] arrStr = new string[5] { "apple""banana""carrot""grape""grapefruit" };
 
            var QueryData =
                from data in arrStr
                where data.Length <= 6
                select data;
 
            foreach (var element in QueryData)
            {
                Console.WriteLine("LINQ DATA :: {0}", element);
            }
 
 
            List<string> strList = new List<string>(arrStr);
            List<string> findAllData = strList.FindAll(a => a.Length <= 6);
 
            foreach (var element in findAllData)
            {
                Console.WriteLine("LAMDBA DATA :: {0}", element);
            }
        }
    }
}