Download - C# 3.0 & LINQ

Transcript
Page 1: C# 3.0  & LINQ

C# 3.0 & LINQ

천호민Visual C# MVPzmeun.tistory.com

Page 2: C# 3.0  & LINQ

Agenda

Part Ⅰ: C# 3.0 New Features

Part Ⅲ : LINQ (Language Integrated Query)

Part Ⅱ : Functional Programming

Page 3: C# 3.0  & LINQ

C# 3.0 New FeaturesVar Local Variable

Anonymous Type

Auto-Implemented Properties

Object and Collection Initializer

Partial Method

Extension Method

Page 4: C# 3.0  & LINQ

Demo: C#3.0 New Features

Page 5: C# 3.0  & LINQ

Functional Programming >>Question…

: What is the Functional Programming?

Answer is …• Paradigm of the Software Programming

• Avoid State and Mutable Data

• So, Contrast with the Imperative Programming Style

( 피하다 ) ( 변하기 쉬운 )

( 대조되다 ,대조하다 )

( 명령적인 )

Page 6: C# 3.0  & LINQ

Functional Programming >>The Foundation Notions…

Higher-Order

Function

First-ClassFunction

Pure Function

Recursion

Non-Stric &Lazy

EvalutionCurry

Closure

Page 7: C# 3.0  & LINQ

Functional Programming >>Higher-Order & First-Class Function

Higher-Order

Function

First-ClassFunction

Pure Function

Recursion

Non-Stric &Lazy

EvalutionCurry

Closure

- 함수를 파라미터로 전달 받고

- 결과를 함수로 리턴

- 수학적인 컨셉과 컴퓨터 과학 용어의 차이

-Closure, Curry 와 관련

Page 8: C# 3.0  & LINQ

Functional Programming >>Pure Function

Higher-Order

Function

First-ClassFunction

Pure Function

Recursion

Non-Stric &Lazy

EvaluationCurry

Closure

- 다른 프로그램에 영향을 주지 않는 개념 (Have No Side-Effect)

- 같은 결과를 리턴하는 함수

- 참조로 전달된 파라미터 값을 수정하지 않고 ,- 참조로 전달된 객체의 멤버 값을 수정하지 않고 ,- 외부 객체 , 클래스의 static 멤버 들을 수정하지 않는다 .

Page 9: C# 3.0  & LINQ

Functional Programming >>Recursion

Higher-Order

Function

First-ClassFunction

Pure Function

Recursion

Non-Stric &Lazy

EvaluationCurry

Closure

- 재귀호출이라 부르며

- 자기 자신을 호출한다 .

- 코드를 단순화 하며

-Higher-Order Function 과 연관

Page 10: C# 3.0  & LINQ

Functional Programming >>Non-Strict & Lazy Evaluation Higher-

OrderFunction

First-ClassFunction

Pure Function

Recursion

Non-Strict &

Lazy Evaluation

Curry

Closure

- 엄격하지 않은 평가 ?, 게으른 평가 ?

Ex) f(x)=x^2 + x + 1, g(x,y)=x + yQ) f(g(1,4))A) 1:f(g(1,4)) -> f(1+4) -> f(5) -> 5^2 + 5 + 1 = 31 2:f(g(1,4))-

>g(1+4)^2+g(1,4)+1 ->(1+4)^2+(1+4)+1-

>5^2+5+1 = 31

- 함수의 파라미터를 식(Expression)

으로 표현- 식의 반복 계산으로 Closure, Lazy Evaluation 필요 .-Lazy Evaluation 은 연산결과가

필요 할때 까지 계산을 지연시키는 기술

Page 11: C# 3.0  & LINQ

Functional Programming >>Closure

Higher-Order

Function

First-ClassFunction

Pure Function

Recursion

Non-Strict &

Lazy Evaluation

Curry

Closure

- 하나의 함수 내 또 다른 함수가 포함되어 있을때 생성

- 외부 함수의 지역 변수를 내부 함수가 참조 가능

- 숨겨진 상태를 처리 . OOP 에서도 사용 가능

- 대게 First-Class Function 에서 사용

Page 12: C# 3.0  & LINQ

Functional Programming >>Curry

Higher-Order

Function

First-ClassFunction

Pure Function

Recursion

Non-Strict &

Lazy Evaluation

Curry

Closure

-Curry? 맛있는 인도 음식 ? No!

- 다중 파라미터를 갖는 함수에서 실행

- 연결된 함수의 리스트

- 함수가 가지는 N 개의 파라미터에 값을 모두 전달하지 않을 경우 함수가 리턴 . 그 외의 경우 함수의 결과 값 리턴 .

-Closure, Higher-Order Function 과 연관

Page 13: C# 3.0  & LINQ

Demo: Functional Programming’s Foundation Notions

Page 14: C# 3.0  & LINQ

- End of Session 1 -

Page 15: C# 3.0  & LINQ

LINQ (Language-Integrated Query) LINQ Archetecture

Lambda Expression

Query Expression & Standard Query Operator

Expression Tree

Deffered Execution

Page 16: C# 3.0  & LINQ

LINQ ArchetectureC# 3.0 Visual Basic

9.0 Others

.NET Language Integrated Query

LINQ toObjects

LINQ toDataSet

sLINQ to

SQLLINQ to

XML

Objects

<book> <title/> <author/> <year/> <price/></book>

XMLRelational

Page 17: C# 3.0  & LINQ

LINQ Lambda Expression

- 식과 문을 포함하고 대리자 (delegate)나

식 트리 (Expression Tree) 형식을 만드는데 사용할 수 있는 익명 함수 .

- C# 프로그래밍 가이드 -

public delegate TResult Func<T, TResult>(T arg)

Syntax : Lambda Operator , “=>”Operator :ex) Func<int, bool> isEven = n => n%2 == 0;

Func<int, int, int> opSum = (n, m) => n+m;

. . . . max argument to 5

Page 18: C# 3.0  & LINQ

LINQ Query Expression & Standard Query Operator

from id in source{ from id in source | join id in source on expr equals expr [ into id ] | let id = expr | where condition | orderby ordering, ordering, … } select expr | group expr by key[ into id query ]

Starts with from Zero or more

from, join, let, where, or orderby

Ends with select or group by

Optional into continuation

Page 19: C# 3.0  & LINQ

LINQ Query Expression & Standard Query Operator

from c in customerswhere c.Region == "WA"select new { c.City, c.Phone };

customers.Where(c => c.Region == "WA").Select(c => new { c.City, c.Phone });

• Queries translate to method invocations• Where, Join, OrderBy, Select, GroupBy, …

Page 20: C# 3.0  & LINQ

LINQ Expression Tree >>

Expression<Predicate<Customer>> test = c => c.Region == "WA";

public delegate bool Predicate<T>(T item);

ParameterExpression c = Expression.Parameter(typeof(Customer), "c");Expression expr = Expression.Equal( Expression.Property(c, typeof(Customer).GetProperty(“Region")), Expression.Constant("WA") );

Expression<Predicate<Customer>> test = Expression.Lambda<Predicate<Customer>>(expr, c);

Page 21: C# 3.0  & LINQ

LINQ Expression Tree >>

c => c.Region == "WA";

“c” Expression.Parameter

“c.Region == ‘WA’” Expression.Equal

Expression.Property

Expression.Constant

“c.Region”

“’WA’”

Page 22: C# 3.0  & LINQ

Demo: Building LINQ in C#3.0

Page 23: C# 3.0  & LINQ

Simplicity C# 3.0 Feature Map

var contacts = from c in customers where c.State == "WA" select new { c.City, c.Phone };

var contacts = customers .Where(c => c.State == "WA") .Select(c => new { c.City, c.Phone });

Extension method Anonymou

s type

Lambda expression

Query expression

Object initializer

Local variable

type inference

Expression tree

Automatic properties

Partial method

Page 24: C# 3.0  & LINQ

Demo: Building LINQ in C#3.0

Continue…

Page 25: C# 3.0  & LINQ

- End of Session 2 -

Page 26: C# 3.0  & LINQ

Resources• MSDN C# 3.0 Overview

http://msdn.microsoft.com/ko-kr/library/bb308966(en-us).aspx

• MSDN .Net Frameworkhttp://msdn.microsoft.com/ko-kr/library/w0x726c2.aspx

• MSDN Linq To SQLhttp://msdn.microsoft.com/ko-kr/library/bb386976.aspx

• MSDN Linq Projecthttp://msdn.microsoft.com/ko-kr/netframework/aa904594(en-us).aspx

• LINQ in Action Bloghttp://linqinaction.net/blogs/default.aspx

• WikiPediahttp://en.wikipedia.org/wiki/Functional_programming

Page 27: C# 3.0  & LINQ

Q&A

Page 28: C# 3.0  & LINQ

Thanks…


Top Related