Lập trình hướng đối tượng 2 - Language Integrated Query (LINQ)

using System; using System.Collections.Generic; namespace Demo01 { class Program { static void Main(string[] args) { string[]greetings={"hello world","hello LINQ","hello Apress" }; List result = new List(); foreach (string greeting in greetings) { if (greeting.EndsWith("LINQ")) { result.Add(greeting); } }

ppt29 trang | Chia sẻ: lylyngoc | Lượt xem: 1475 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Lập trình hướng đối tượng 2 - Language Integrated Query (LINQ), để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG 2 Language Integrated Query (LINQ) Nội dung Giới thiệu LINQ LINQ to Object LINQ to XML LINQ to ADO.NET Giới thiệu LINQ using System; using System.Collections.Generic; namespace Demo01 { class Program { static void Main(string[] args) { string[]greetings={"hello world","hello LINQ","hello Apress" }; List result = new List(); foreach (string greeting in greetings) { if (greeting.EndsWith("LINQ")) { result.Add(greeting); } } foreach (string item in result) { Console.WriteLine(item); } Console.ReadLine(); } } } Trước khi có LINQ Giới thiệu LINQ using System; using System.Linq; namespace Demo01 { class Program { static void Main(string[] args) { string[]greetings = {"hello world", "hello LINQ", "hello Apress" };   var items = from s in greetings where s.EndsWith("LINQ") select s;   foreach (var item in items) Console.WriteLine(item); } } } Khi có LINQ Giới thiệu LINQ Language Integrated Query (LINQ) là ngôn ngữ truy vấn hợp nhất trên các loại dữ liệu khác nhau. Với LINQ, bạn có thể truy vấn nhiều nguồn dữ liệu khác nhau trong C#: đối tượng (object), cơ sở dữ liệu SQL, tài liệu XML, mô hình dữ liệu thực thể (entity data model). Đưa ra khả năng lập trình mới trong .NET - Giải pháp lập trình hợp nhất LINQ provides one programming model for all types of data (objects, SQL, XML, DataSets) Giới thiệu LINQ Giới thiệu LINQ Tất cả các thao tác truy vấn LINQ gồm 3 hành động chính: Lấy nguồn dữ liệu Tạo truy vấn Thực thi truy vấn LINQ to Object Sử dụng LINQ để truy vấn tập hợp các đối tượng dưới dạng IEnumerable hoặc IEnumerable Ví dụ: int[ ] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; List list; string[] str = { "Visual Studio 2008", "LINQ", "WCF", "WWF", "WPF"}; LINQ to Object static void Main(string[] args) { int[ ] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums = from n in numbers where n customers = new List { new Customer { ID="A", City="New York", Country="USA", Region="North America", Sales=9999 }, new Customer { ID="B", City="Mumbai", Country="India", Region="Asia", Sales=8888 }, new Customer { ID="C", City="Karachi", Country="Pakistan", Region="Asia", Sales=7777 }}; var queryResults = from c in customers where c.Region == "Asia" select c; foreach (Customer c in queryResults) { Console.WriteLine(c); } } LINQ to XML 1 Sách lập trình C# 1 400000 2 Sách lập trình VB 1 50000 LINQ to XML Cung cấp 1 công cụ mạnh trong việc truy vấn XML var sp = (from c in XElement.Load("SanPham.xml").Elements("SanPham") select new { MaSanPham = (string)c.Element("MaSanPham"), TenSanPham = (string)c.Element("TenSanPham"), MaLoai = (string)c.Element("MaLoai"), DonGia = (string)c.Element("DonGia"), }).ToArray(); dgSanPham.DataSource = sp; LINQ to DataSet LINQ to DataSet giúp truy vấn đối tượng Dataset dễ dàng và nhanh chóng string str = "server = localhost; database = QLBH; uid=sa; pwd = 123456"; SqlConnection con = new SqlConnection(str); con.Open(); SqlDataAdapter da = new SqlDataAdapter("Select * From SanPham", con); DataSet ds = new DataSet(); da.Fill(ds, "SanPham"); var sp = (from sanpham in ds.Tables["SanPham"].AsEnumerable() select new {MaSanPham=sanpham["MaSanPham"], TenSanPham=sanpham["TenSanPham"], MaLoai=sanpham["MaLoai"], DonGia=sanpham["DonGia"]}).ToArray(); dgSanPham.DataSource = sp; con.Close(); LINQ to SQL LINQ to SQL là một phiên bản Object -Relational Mapping (ORM). LINQ to SQL – Lớp DataContext Là một lớp kết nối đến CSDL Chuyển câu truy vấn thành câu lệnh SQL Đảm nhận việc tương tác với CSDL Thay đổi CSDL thông qua phương thức SubmitChanges() LINQ to SQL Ví dụ: Cấu trúc LINQ to SQL from c in db.Customers where c.City == "London" select c.CompanyName Enumerate SELECT CompanyName FROM Customer WHERE City = 'London' SQL Query or SProc Rows Objects db.Customers.Add(c1); c2.City = “Seattle"; db.Customers.Remove(c3); SubmitChanges() INSERT INTO Customer … UPDATE Customer … DELETE FROM Customer … DML( Data Manipulation Language) or SProcs Ví dụ: Quản lý bán hàng Ví dụ: Quản lý bán hàng Ví dụ: Quản lý bán hàng Ví dụ: Quản lý bán hàng Ví dụ: Quản lý bán hàng Thiết kế giao diện Ví dụ: Quản lý bán hàng Load_SanPham() DataClassesDataContext data = new DataClassesDataContext(); var listsanpham = from sanpham in data.SanPhams select new { sanpham.MaSanPham, sanpham.TenSanPham, sanpham.MaLoai, sanpham.DonGia }; dgSanPham.DataSource = listsanpham; Ví dụ: Quản lý bán hàng Insert_SanPham() DataClassesDataContext data = new DataClassesDataContext(); SanPham sp = new SanPham(); sp.MaSanPham = txtMaSanPham.Text.Trim(); sp.TenSanPham = txtTenSanPham.Text; sp.MaLoai = cboLoai.SelectedValue.ToString(); sp.DonGia =Convert.ToDecimal(txtDonGia.Text); data.SanPhams.InsertOnSubmit(sp); data.SubmitChanges(); Ví dụ: Quản lý bán hàng Delete_SanPham() DataClassesDataContext data = new DataClassesDataContext(); SanPham sp = (from sanpham in data.SanPhams where sanpham.MaSanPham == txtMaSanPham.Text.Trim() select sanpham).Single(); data.SanPhams.DeleteOnSubmit(sp); data.SubmitChanges(); Ví dụ: Quản lý bán hàng Update_SanPham() DataClassesDataContext data = new DataClassesDataContext(); SanPham sp = (from sanpham in data.SanPhams where sanpham.MaSanPham == txtMaSanPham.Text.Trim() select sanpham).Single(); sp.MaSanPham = txtMaSanPham.Text; sp.TenSanPham = txtTenSanPham.Text; sp.MaLoai = cboLoai.SelectedValue.ToString(); sp.DonGia = Convert.ToDecimal(txtDonGia.Text); data.SubmitChanges(); LINQ vs ADO.NET LINQ là tập mở rộng cho phép viết các câu truy vấn ngay trong các ngôn ngữ lập trình. ADO.NET là công nghệ cho phép các ứng dụng có thể kết nối và làm việc với các loại CSDL khác nhau LINQ không phải là một công nghệ được tạo ra để thay thế ADO.NET LINQ và 3-Layers DataClassesDataContext() var query = from …… where…… select …… Nhiệm vụ về nhà Làm lại bài tập QLBH bằng LINQ to SQL Nghiên cứu XML và LINQ to XML Đọc thêm tài liệu LINQ để hiểu rõ cú pháp
Tài liệu liên quan