14
Introduction to LINQ Reporter : Peter

20130329 introduction to linq

Embed Size (px)

DESCRIPTION

Introduction LINQ by Peter

Citation preview

Page 1: 20130329 introduction to linq

Introduction to LINQ

Reporter : Peter

Page 2: 20130329 introduction to linq

What is LINQ

• LINQ : language integrated query

• 可以使用一種相同的查詢方式來查詢不同的資料來源• 有 LINQ TO OBJECTS, LINQ TO XML LINQ TO SQL

• 有兩種寫法 : query syntax , method syntax

Page 3: 20130329 introduction to linq
Page 4: 20130329 introduction to linq

• Query syntax寫好的 linq statement最終都會被 compile 成 method syntax

(因為 .net CLR 無法理解 query syntax)

常用的method syntax 有 :Where, Select, GroupBy, Join, Max, Average

In general, we recommend query syntax because it is usually simpler and more readable; however there is no semantic difference between method syntax and query syntax. In addition, some queries, such as those that retrieve the number of elements that match a specified condition, or that retrieve the element that has the maximum value in a source sequence, can only be expressed as method calls

Page 5: 20130329 introduction to linq

新型態 var

• Var是一種後決議型別 (compile時才知道他是屬於哪種類型 )

• 它的型別是由右方的運算式運算過後才可得知

• 所以在無法事先預知查詢 statement會回傳甚麼型態時 ,就可以使用 var取代真正的 type(for annoymous class)

Page 6: 20130329 introduction to linq
Page 7: 20130329 introduction to linq

What is extension method(since c#3.0)

Page 8: 20130329 introduction to linq

Lambda expression

• To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator =>, and you put the expression or statement block on the other side.

• For example, the lambda expression x => x * x specifies a parameter that’s named x and returns the value of x squared.

Page 9: 20130329 introduction to linq

• the Where method: Where(num => num % 2 == 0). This inline expression is called a lambda expression. It is a convenient way to write code that would otherwise have to be written in more cumbersome form as an anonymous method or a generic delegate or an expression tree. In C# => is the lambda operator, which is read as "goes to". The num on the left of the operator is the input variable which corresponds to num in the query expression. The compiler can infer the type of num because it knows that numbers is a generic IEnumerable<T> type. The body of the lambda is just the same as the expression in query syntax or in any other C# expression or statement; it can include method calls and other complex logic. The "return value" is just the expression result.

• To get started using LINQ, you do not have to use lambdas extensively. However, certain queries can only be expressed in method syntax and some of those require lambda expressions. After you become more familiar with lambdas, you will find that they are a powerful and flexible tool in your LINQ toolbox.

Page 10: 20130329 introduction to linq

Expression Lambdas

• A lambda expression with an expression on the right side is called an expression lambda.

• ( input parameters ) => expression

• Ex: ( x, y ) => x + y

• (int x, string s ) => s.Length > x

Page 11: 20130329 introduction to linq
Page 12: 20130329 introduction to linq
Page 13: 20130329 introduction to linq
Page 14: 20130329 introduction to linq