@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[] { 0, 54, 98, 102, 332 };
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);
}
}
}
}
|
'C# > 복습' 카테고리의 다른 글
2020-05-26 LINQ에 대해 더 알아보기(select, orderby, group, join) (0) | 2020.05.27 |
---|---|
2020-05-17 델리게이트, 선언방법, 활용법, 이벤트 (1) | 2020.05.17 |
2020-05-02 이름 자동으로 섞기 (0) | 2020.05.02 |
2020-04-30 삼항연산자, Null병합연산자, 연산자 우선순위 (0) | 2020.04.30 |
2020-04-28 저장 예제만들어 복습 (0) | 2020.04.29 |