Introduction to LINQ
       Reporter : Peter
What is LINQ
•   LINQ : language integrated query
•   可以使用一種相同的查詢方式來查詢不同的資料來源
•   有 LINQ TO OBJECTS, LINQ TO XML LINQ TO SQL
•   有兩種寫法 : query syntax , method syntax
• 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
新型態 var
• Var 是一種後決議型別 (compile 時才知道他是屬於哪種類型 )


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


• 所以在無法事先預知查詢 statement 會回傳甚麼型態時 , 就可以使用
 var 取代真正的 type(for annoymous class)
What is extension method(since c#3.0)
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.
• 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.
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
20130329 introduction to linq
20130329 introduction to linq
20130329 introduction to linq
20130329 introduction to linq

20130329 introduction to linq

  • 1.
    Introduction to LINQ Reporter : Peter
  • 2.
    What is LINQ • LINQ : language integrated query • 可以使用一種相同的查詢方式來查詢不同的資料來源 • 有 LINQ TO OBJECTS, LINQ TO XML LINQ TO SQL • 有兩種寫法 : query syntax , method syntax
  • 4.
    • 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
  • 5.
    新型態 var • Var是一種後決議型別 (compile 時才知道他是屬於哪種類型 ) • 它的型別是由右方的運算式運算過後才可得知 • 所以在無法事先預知查詢 statement 會回傳甚麼型態時 , 就可以使用 var 取代真正的 type(for annoymous class)
  • 7.
    What is extensionmethod(since c#3.0)
  • 8.
    Lambda expression • Tocreate 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.
  • 9.
    • 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.
  • 10.
    Expression Lambdas • Alambda 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

Editor's Notes

  • #7 Example of method syntax better over query syntax
  • #15 IGROUPING