Bài giảng Phân tích thiết kế phần mềm LINQ - Ngô Ngọc Đăng Khoa

Phân tích thiết kế phần mềm  
LINQ  
Ngô Ngọc Đăng Khoa  
1
Intro  
• LINQ đọc là LINK, không phải LIN-QUEUE  
• LINQ: Language Integrated Query  
• LINQ cho phép developer thực hiện truy  
vấn trên nhiều dạng dữ liệu trong .NET  
– .NET Objects (List, Queue, Array, …)  
– Database (DLINQ)  
– XML (XLINQ)  
– Parallel LINQ (PLINQ)  
2
Intro  
DLINQ  
LINQ  
XLINQ  
PLINQ  
3
C#  
VB  
Others…  
.NET Language Integrated Query  
Standard  
Query  
Operators  
DLinq  
(ADO.NET)  
XLinq  
(System.Xml)  
<book>  
<title/>  
<author/>  
<year/>  
<price/>  
</book>  
Objects  
XML  
Database  
4
LINQ TO OBJECTS  
5
1st Example  
var list = new List<int>() {1, 2, 3};  
var query = from n in list  
where n < 3  
select n;  
foreach (var n in query)  
Console.WriteLine(n);  
6
.NET 3.0+ Features  
Implicitly typed local variables  
var query =  
from n in list  
where n < 3  
select n;  
Ienumerable<int> query = from n in list  
where n < 3  
select n;  
7
.NET 3.0+ Features  
Collection Initializers  
List<int> list = new List<int>();  
list.Add(1);  
list.Add(2);  
list.Add(3);  
List<int> list = new List<int>() {1, 2, 3};  
8
.NET 3.0+ Features  
Dictionary Initializers  
Dictionary<int, string> dic =  
new Dictionary<int, string>();  
dic.Add(1, “value1”);  
dic.Add(2, “value2”);  
dic.Add(3, “value3”);  
Dictionary<int, string> dic =  
new Dictionary<int, string> {  
{1, “value1”}, {2, “value2”}  
};  
9
Query Syntax  
from n in list  
where n < 3  
select n;  
foreach (int n in list)  
{
if (n < 3) //xử lý n  
}
10  
Quiz  
• List<int> list = Enumerable.Range(1, 100);  
• A) Tìm các số có tận cùng là 4  
• B) Tìm các số chia hết cho 3  
• Thử viết hai cách, dùng hàm và LINQ  
11  
2nd Example  
Truy vấn trên đối tượng  
public class Customer  
{
public string CustomerID { get; set; }  
public string ContactName { get; set; }  
public string City { get; set; }  
}
12  
.NET 3.0+ Features  
Automatic Properties  
string _data;  
public string Data  
{
get { return _data; }  
set { _data = value; }  
}
public string Data { get; set; }  
13  
2nd Example (cont)  
static List<Customer> GetCustomers()  
{
return new List<Customer> {  
new Customer { CustomerID = "ALFKI", ContactName =  
"Maria Anders", City = "Berlin" },  
new Customer { CustomerID = "ANATR", ContactName = "Ana  
Trujillo", City = "Mexico D.F." },  
new Customer { CustomerID = "ANTON", ContactName =  
"Antonino Moreno", City = "Mexico D.F." }  
};  
}
Customer c = new Customer();  
c.CustomerID = "ALFKI";  
c.ContactName = "Maria Anders";  
c.City = "Berlin";  
14  
.NET 3.0+ Features  
Object Initializers  
class MyClass  
{
public string Prop1 { get; set; }  
public string Prop2 { get; set; }  
}
MyClass c = new MyClass {  
MyClass c = new MyClass();  
c.Prop1 = “Value1”;  
c.Prop2 = “Value2”;  
Prop1 = “Value1”,  
Prop2 = “Value2”  
};  
15  
2nd Example (cont)  
var query = from c in GetCustomers()  
where c.City == "Mexico D.F.”  
//where c.City.StartWith(“A”)  
select new {  
City = c.City,  
ContactName = c.ContactName  
};  
foreach (var c in query)  
{
//Xuất thông tin c  
}
Quiz  
• Product(SKU, Name, Quantity, Price)  
• ProductDAO  
– List<Product> GetAll();  
• Cho biết tên các mặt hàng còn trong kho  
• Cho biết tên, giá các mặt hàng sắp hết (Số  
lượng <10)  
• Cho biết các mặt hàng có giá từ 100 đến  
200  
17  
.NET 3.0+ Features  
Anonymous Type  
var dude = new { Name = “Bob”, Age = 25 };  
internal class AnonymousGeneratedTypeName  
{
public string Name { get; set; }  
public int Age { get; set; }  
}
AnonymousGeneratedTypeName dude =  
new AnonymousGeneratedTypeName {Name = “Bob”, Age = 25};  
18  
Query syntax – Lồng nhau  
19  
Query Syntax – let  
var list =  
new List<int> { 1,2,3,4,5,6,7,8,9 };  
var query = from n in list  
where n > 3 && n < 8  
let g = n * 2  
let newList = new List<int> {1,2,3}  
from l in newList  
select new { l, r = g * l };  
Hướng xử lí: trong ra ngoài  
20  
Tải về để xem bản đầy đủ
pptx 136 trang myanh 30160 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Phân tích thiết kế phần mềm LINQ - Ngô Ngọc Đăng Khoa", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pptxbai_giang_phan_tich_thiet_ke_phan_mem_linq_ngo_ngoc_dang_kho.pptx