... | ... | @@ -217,17 +217,19 @@ class Program |
|
|
|
|
|
2. 데이터로서의 람다 식
|
|
|
식을 표현한 데이터로 expression tree라고 한다.
|
|
|
|
|
|
```cs
|
|
|
Expression<Func<int, bool>> exprTree = num => num < 5;
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
Expression<Func<int, bool>> exprTree = num => num < 5;
|
|
|
|
|
|
// Decompose the expression tree.
|
|
|
ParameterExpression param = (ParameterExpression)exprTree.Parameters[0];
|
|
|
BinaryExpression operation = (BinaryExpression)exprTree.Body;
|
|
|
ParameterExpression left = (ParameterExpression)operation.Left;
|
|
|
ConstantExpression right = (ConstantExpression)operation.Right;
|
|
|
// Decompose the expression tree.
|
|
|
ParameterExpression param = (ParameterExpression)exprTree.Parameters[0];
|
|
|
BinaryExpression operation = (BinaryExpression)exprTree.Body;
|
|
|
ParameterExpression left = (ParameterExpression)operation.Left;
|
|
|
ConstantExpression right = (ConstantExpression)operation.Right;
|
|
|
|
|
|
Console.WriteLine("Decomposed expression: {0} => {1} {2} {3}", param.Name, left.Name, operation.NodeType, right.Value);
|
|
|
Console.WriteLine("Decomposed expression: {0} => {1} {2} {3}", param.Name, left.Name, operation.NodeType, right.Value);
|
|
|
}
|
|
|
```
|
|
|
|
|
|
* **람다 식을 위한 전용 delegate :**람다 식은 기존 method와 달리 일회성으로 사용되는 간단한 코드를 표현할 때 사용되는데, 정작 그러한 목적으로 delegate를 일일이 정의해야한다는 불편함이 발생한다. 마이크로소프트에서는 이러한 불편을 덜기 위해 자주 사용되는 delegate형식을 generic의 도움으로 일반화해서 BCL에 Action, Func로 포함시켰다.
|
... | ... | @@ -243,7 +245,6 @@ Console.WriteLine("Decomposed expression: {0} => {1} {2} {3}", param.Name, left. |
|
|
|
|
|
`[생략 : T1 ~ T16까지 Action, Func delegate 정의]`
|
|
|
|
|
|
* **collection과 람다 식**
|
|
|
* **람다식으로 Event를 구현하기**
|
|
|
Event의 [Event 사용법](#first)예제에서 callback method 부분을 아래와 같이 람다 식을 사용해서 구현할 수 있다.
|
|
|
```cs
|
... | ... | @@ -344,7 +345,7 @@ public static IEnumerable<TSource> Where<TSource>( |
|
|
Func<TSource, bool> predicate
|
|
|
)
|
|
|
```
|
|
|
predicate 값에 따라 value들의 sequence를 filtering한다.
|
|
|
predicate 값에 따라 value를 filtering한다.
|
|
|
|
|
|
```cs
|
|
|
List<string> fruits = new List<string> { "apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry" };
|
... | ... | @@ -374,7 +375,7 @@ foreach (int fruit in query) |
|
|
Console.WriteLine(fruit);
|
|
|
}
|
|
|
```
|
|
|
## 5. **LINQ 기본 문법 정리**
|
|
|
## **5. LINQ 기본 문법 정리**
|
|
|
|
|
|
**1. where**
|
|
|
LINQ의 where를 통해 `IEnumerable<T>` type인 list collection에서 특정 조건을 만족하는 data를 filtering 할 수 있다. where의 조건절에는 return type으로 bool type을 만족하는 어떤 C#코드도 올 수 있다.
|
... | ... | |