Danh mục

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

Số trang: 93      Loại file: pptx      Dung lượng: 4.11 MB      Lượt xem: 15      Lượt tải: 0    
10.10.2023

Xem trước 10 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

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.
Nội dung trích xuất từ tài liệu:
Bài giảng Công nghệ lập trình tiên tiến: Chương 2 - ĐH Công nghệ Đồng Nai DONG NAI UNIVERSITY OF TECHNOLOGY LINQ Language Integrated Query 1 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 DONG NAI UNIVERSITY OF TECHNOLOGY 2. New features in language: Ø Generics Ø Implicitly Typed Variables Ø Object Initializers Ø Anonymous Types Ø Extension Methods Ø Lambda Expressions 3 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 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 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 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 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 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 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 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 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 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 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 DONG NAI UNIVERSITY OF TECHNOLOGY 2.4. Extension Methods Ø Example MessageBox.Show(n.SumFrom1toN()+); 15 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 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)); } ...

Tài liệu được xem nhiều: