intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài giảng Công nghệ lập trình tiên tiến: Chương 2 - ĐH Công nghệ Đồng Nai

Chia sẻ: Na Na | Ngày: | Loại File: PPTX | Số trang:93

82
lượt xem
9
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Bài giảng Công nghệ lập trình tiên tiến: Chương 2 trình bày các nội dung kiến thức về truy xuất dữ liệu với LINQ như các thao tác trên máy tính, câu lệnh, cú pháp,... Tham khảo nội dung bài giảng để hiểu rõ hơn về các nội dung trên.

Chủ đề:
Lưu

Nội dung Text: Bài giảng Công nghệ lập trình tiên tiến: Chương 2 - ĐH Công nghệ Đồng Nai

  1. DONG NAI UNIVERSITY OF TECHNOLOGY LINQ Language Integrated Query 1
  2. DONG NAI UNIVERSITY OF TECHNOLOGY 1. Basic concepts & Types LinQ Ø Basic concepts ü LINQ requirements ü Concepts Ø Types ü LINQ to objects ü LINQ to SQL ü LINQ to Entity ü LINQ to XML ü LINQ to Dataset 2
  3. DONG NAI UNIVERSITY OF TECHNOLOGY 2. New features in language: Ø Generics Ø Implicitly Typed Variables Ø Object Initializers Ø Anonymous Types Ø Extension Methods Ø Lambda Expressions 3
  4. DONG NAI UNIVERSITY OF TECHNOLOGY 2.0. Generics Ø The creation of various types of collection Ø Type safety Ø Binary Code Reuse using System.Collections.Generic; List intS = new List(); intS.Add(113); intS.Add(114); int nAt = intS[0]; 4
  5. DONG NAI UNIVERSITY OF TECHNOLOGY 2.1. Implicitly Typed Variables Ø Implicitly Typed Variables – Declare variables without specifying their type – Strongly type, not variant, not object – Visual Studio will determine type • Predict what the compiler will choose • Intelligence support – Type inference -> most general • “3/10/2010” -> string, not date 5
  6. DONG NAI UNIVERSITY OF TECHNOLOGY 2.1. Implicitly Typed Variables Ø Example private void button2_Click(object sender, EventArgs e) { var x = 113; var y = "1/1/2012"; var z = 1.7; var k = new DateTime(2012, 1, 1); string msg = "x type="+x.GetType() + "\n"+ "y type = "+y.GetType() + "\n" + "z type ="+z.GetType() + "\n" + "k type = " + k.GetType(); MessageBox.Show(msg); 6
  7. DONG NAI UNIVERSITY OF TECHNOLOGY 2.1. Implicitly Typed Variables Ø Note – Always declare the type if we know – Implicitly Typed Variables are suited for LINQ and anonymous type 7
  8. DONG NAI UNIVERSITY OF TECHNOLOGY 2.2. Object Initializers Ø Constructor Ø Allow to assign values to object properties (fields) when create object Ø We do not have to explicitly invoke a constructor Ø Useful in any context – Especially useful in LINQ expressions 8
  9. DONG NAI UNIVERSITY OF TECHNOLOGY 2.2. Object Initializers Ø Example class Test { public int a, b; public int a2 { set;get;} public int b2 { get; set; } } private void button3_Click(object sender, EventArgs e) { var x = new Test() {a=113,b=114,a2=115,b2=116}; MessageBox.Show(x.a2+""); 9
  10. DONG NAI UNIVERSITY OF TECHNOLOGY 2.3. Anonymous Types Ø Implicitly type functionality for objects – Set property values to object without writing class definition – The resulting class has no usable name – Class name is generated by compiler, inherits from Object – The result: an anonymous type that is not available at source code level Ø Also called Projections 10
  11. DONG NAI UNIVERSITY OF TECHNOLOGY 2.3. Anonymous Types Ø When to user anonymous types – Need a temporary object to hold related data – Don’t need method – If we need a different set of properties for each declaration – If we need to change the order of properties for each declaration 11
  12. DONG NAI UNIVERSITY OF TECHNOLOGY 2.3. Anonymous Types Ø When not to user anonymous types – Need to define methods – Need to define another variable – Need to shared data across methods private void btn4_Click(object sender, EventArgs e) { var teo = new { ID=1234,Name="Tèo Hả Tèo"}; MessageBox.Show(teo.ID +"-"+teo.Name); } 12
  13. DONG NAI UNIVERSITY OF TECHNOLOGY 2.4. Extension Methods Ø Special kind of Static method Ø Allow the addition of methods to an existing class – Without creating a new derived type – Without re-compiling or modifying the original type Ø Called The Same way regular methods are called Ø Define in static class 13
  14. DONG NAI UNIVERSITY OF TECHNOLOGY 2.4. Extension Methods Ø Example namespace StudyLinQ { public static class MyExtensionMethod { public static int SumFrom1toN(this int n) { int sum = 0; for (int i = 1; i
  15. DONG NAI UNIVERSITY OF TECHNOLOGY 2.4. Extension Methods Ø Example MessageBox.Show(n.SumFrom1toN()+""); 15
  16. DONG NAI UNIVERSITY OF TECHNOLOGY 2.4. Extension Methods Ø Example namespace StudyLinQ { public static class MyExtensionMethod { public static int SumFrom1toN(this int n){…} public static string NoiChuoi(this string s1, string s2) { return s1 + "-" + s2; } } } 16
  17. DONG NAI UNIVERSITY OF TECHNOLOGY 2.4. Extension Methods Ø Example private void btnNoi_Click (object sender, EventArgs e) { string s = "Tý"; MessageBox.Show( s.NoiChuoi("Mập")); } 17
  18. DONG NAI UNIVERSITY OF TECHNOLOGY 2.4. Extension Methods using System.Windows.Forms; Ø Example using System.Drawing; namespace StudyLinQ { public static class MyExtensionMethod { public static int SumFrom1toN(this int n) {… } public static string NoiChuoi(this string s1, string s2) {…} public static void ChangeColorToRed(this Button btn) { btn.BackColor = Color.Red; } } } 18
  19. DONG NAI UNIVERSITY OF TECHNOLOGY 2.4. Extension Methods Ø Example btnColor.ChangeColorToRed(); 19
  20. DONG NAI UNIVERSITY OF TECHNOLOGY 2.4.a. Delegate Ø Delegate – refers to method. – When initialize a delegate, we initialize it with method. Ø Example //Defines a delegate public delegate int ChangeInt(int x); public int Tang2(int x){return x + 2;} public int Giam2(int x) {return x - 2;} 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2