SlideShare a Scribd company logo
1 of 22
Download to read offline
LINQ TrầnChí Khang Microsoft Student Partners Email: khangtran1990@hotmail.com
NỘIDUNG ,[object Object]
Phân loại
LINQ to Object
LINQ to XML
LINQ to Dataset
LINQ to SQL
ADO.NET
ADO.NET EntityFramework
LINQ vs ADO.NET
DEMO,[object Object]
XML files
File  (Access, Excel …)Cách giao tiếp ?
KHÁI NIỆM LINQ ,[object Object]
Đư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,[object Object]
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<T> Without LINQ With LINQ List<Students> MaleStudents = new List<Students>(); Foreach(Students student in Students) {   if(student.sex == “male”)   {     MaleStudents.Add(student	);   } } IEnumerable <Students> MaleStudents = from student in Students where student.sex==“male” select student;
LINQ TO XML  XmlDocumentdoc = newXmlDocument(); XmlElementbooks = doc.CreateElement("books"); XmlElement author = doc.CreateElement("author"); author.InnerText = "Fabrice Marguerie"; XmlElement price= doc.CreateElement(“price"); author2.InnerText = “80”; XmlElement publishyear = doc.CreateElement(“publishyear"); author3.InnerText = “2008"; XmlElementtitle = doc.CreateElement("title"); title.InnerText = “ABC"; XmlElementbook = doc.CreateElement("book"); book.AppendChild(author1); book.AppendChild(author2); book.AppendChild(author3); book.AppendChild(title); books.AppendChild(book); doc.AppendChild(books); XDocument xmlbooks = newXDocument( newXDeclaration("1.0", "UTF-16", "yes"), newXElement("books", newXElement("book", newXAttribute("id","B01"), newXElement("author", "Fabrice Marguerie"), newXElement("price", 80), newXElement("publishyear", 2008), newXElement("title", "ABC")))); <?xmlversion="1.0"encoding="utf-8" ?> <books>   <book id = “B01”>     <title>ABC</title>     <author>Fabrice Marguerie </author> <price>80</price> <pulishyear>2008</pulishyear>       </book> </books>
LINQ TO XML  Cung cấp 1 công cụ mạnh mẽ trong việc truy vấnXML var books = from b in xmlbooks.Elements("book") 	     where(String)b.Element("price") == 80                            selectnew                 {                     title = (String)b.Element("tilte"),                     author = (String)b.Element("author")                 }; foreach (var book in books)             { Console.WriteLine(book); }
LINQ TO DATASET DATASET ? LINQ to DataSet giúp truy vấn đối tượng Dataset dễ dàng và nhanh chóng var cus = from c in customersDataset.Tables["Customers"].AsEnumerable()           select c["CustomerID"]; foreach (var c in cus) {    Console.WriteLine("{0}", c); }
LINQ TO SQL LINQ to SQL là một phiên bản Object-Relational Mapping (ORM). Database DataContext Table Class View Class Column Field / Property Relationship Field / Property Stored Procedure Method
LớpDataContext ,[object Object]
Chuyển câu truy vấn thành câu lệnh SQL

More Related Content

Viewers also liked

Summary for the first evaluation
Summary for the first evaluationSummary for the first evaluation
Summary for the first evaluationmartamiren
 
Lec2 MECH ENG STRucture
Lec2    MECH ENG  STRuctureLec2    MECH ENG  STRucture
Lec2 MECH ENG STRuctureMohamed Yaser
 
Proposed Changes in General-Season Deer Hunting — Dec. 2, 2010 Meeting
Proposed Changes in General-Season Deer Hunting  — Dec. 2, 2010 MeetingProposed Changes in General-Season Deer Hunting  — Dec. 2, 2010 Meeting
Proposed Changes in General-Season Deer Hunting — Dec. 2, 2010 MeetingUtah Division of Wildlife Resources
 
Подарим детям сказку
Подарим детям сказкуПодарим детям сказку
Подарим детям сказкуNikita Kudelin
 
Lec6 MECH ENG STRucture
Lec6   MECH ENG  STRuctureLec6   MECH ENG  STRucture
Lec6 MECH ENG STRuctureMohamed Yaser
 
Tbadv492 good version
Tbadv492 good versionTbadv492 good version
Tbadv492 good versionZach Chmill
 
Uni masr.com f29d8aea7936d9edc04dafcd2ad1b027
Uni masr.com f29d8aea7936d9edc04dafcd2ad1b027Uni masr.com f29d8aea7936d9edc04dafcd2ad1b027
Uni masr.com f29d8aea7936d9edc04dafcd2ad1b027Mohamed Yaser
 
Taco Bell ADV492
Taco Bell ADV492Taco Bell ADV492
Taco Bell ADV492Zach Chmill
 
حياة بلا توتر
حياة بلا توترحياة بلا توتر
حياة بلا توترMohamed Yaser
 
What is nanotechnology
What is nanotechnologyWhat is nanotechnology
What is nanotechnologyMohamed Yaser
 
Exraindo informações de negócio a partir de logs de aplicações dom o ELK
Exraindo informações de negócio a partir de logs de aplicações dom o ELKExraindo informações de negócio a partir de logs de aplicações dom o ELK
Exraindo informações de negócio a partir de logs de aplicações dom o ELKMarcus Vinicius Leandro
 

Viewers also liked (20)

Lec06
Lec06Lec06
Lec06
 
Summary for the first evaluation
Summary for the first evaluationSummary for the first evaluation
Summary for the first evaluation
 
Neurocirugía; Marzo 2011, Volume 16
Neurocirugía; Marzo 2011, Volume 16Neurocirugía; Marzo 2011, Volume 16
Neurocirugía; Marzo 2011, Volume 16
 
Lec2 MECH ENG STRucture
Lec2    MECH ENG  STRuctureLec2    MECH ENG  STRucture
Lec2 MECH ENG STRucture
 
Proposed Changes in General-Season Deer Hunting — Dec. 2, 2010 Meeting
Proposed Changes in General-Season Deer Hunting  — Dec. 2, 2010 MeetingProposed Changes in General-Season Deer Hunting  — Dec. 2, 2010 Meeting
Proposed Changes in General-Season Deer Hunting — Dec. 2, 2010 Meeting
 
Подарим детям сказку
Подарим детям сказкуПодарим детям сказку
Подарим детям сказку
 
فن التفاوض
فن التفاوضفن التفاوض
فن التفاوض
 
Lec6 MECH ENG STRucture
Lec6   MECH ENG  STRuctureLec6   MECH ENG  STRucture
Lec6 MECH ENG STRucture
 
Sgcha slide share
Sgcha slide shareSgcha slide share
Sgcha slide share
 
Tbadv492 good version
Tbadv492 good versionTbadv492 good version
Tbadv492 good version
 
Uni masr.com f29d8aea7936d9edc04dafcd2ad1b027
Uni masr.com f29d8aea7936d9edc04dafcd2ad1b027Uni masr.com f29d8aea7936d9edc04dafcd2ad1b027
Uni masr.com f29d8aea7936d9edc04dafcd2ad1b027
 
Problems 2
Problems 2Problems 2
Problems 2
 
Neurocirugía Noviembre 2011 (Vol 17)
Neurocirugía Noviembre 2011 (Vol 17)Neurocirugía Noviembre 2011 (Vol 17)
Neurocirugía Noviembre 2011 (Vol 17)
 
Taco Bell ADV492
Taco Bell ADV492Taco Bell ADV492
Taco Bell ADV492
 
Juniper marketing
Juniper marketingJuniper marketing
Juniper marketing
 
หนังสือแจ้งโรงเรียน 11
หนังสือแจ้งโรงเรียน 11หนังสือแจ้งโรงเรียน 11
หนังสือแจ้งโรงเรียน 11
 
حياة بلا توتر
حياة بلا توترحياة بلا توتر
حياة بلا توتر
 
What is nanotechnology
What is nanotechnologyWhat is nanotechnology
What is nanotechnology
 
Exraindo informações de negócio a partir de logs de aplicações dom o ELK
Exraindo informações de negócio a partir de logs de aplicações dom o ELKExraindo informações de negócio a partir de logs de aplicações dom o ELK
Exraindo informações de negócio a partir de logs de aplicações dom o ELK
 
Tutorial 2
Tutorial     2Tutorial     2
Tutorial 2
 

Similar to LINQ presentation

Bài 6: Lập trình với CSDL Kiến trúc kết nối & Buộc dữ liệu - Lập trình winfor...
Bài 6: Lập trình với CSDL Kiến trúc kết nối & Buộc dữ liệu - Lập trình winfor...Bài 6: Lập trình với CSDL Kiến trúc kết nối & Buộc dữ liệu - Lập trình winfor...
Bài 6: Lập trình với CSDL Kiến trúc kết nối & Buộc dữ liệu - Lập trình winfor...MasterCode.vn
 
Hdth07 ltudql02-linq-ep1
Hdth07 ltudql02-linq-ep1Hdth07 ltudql02-linq-ep1
Hdth07 ltudql02-linq-ep1Dũng Đinh
 
Web Services
Web ServicesWeb Services
Web Servicesask bills
 
2.gioi thieu co ban ado.net cho nguoi lap trinh c#
2.gioi thieu co ban ado.net cho nguoi lap trinh c#2.gioi thieu co ban ado.net cho nguoi lap trinh c#
2.gioi thieu co ban ado.net cho nguoi lap trinh c#Dao Uit
 
Net06 asp.net applications & state management
Net06 asp.net applications & state managementNet06 asp.net applications & state management
Net06 asp.net applications & state managementhoangnguyentien
 
Bài 5: Làm quen với lập trình CSDL ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
Bài 5: Làm quen với lập trình CSDL ASP.NET - Giáo trình FPT - Có ví dụ kèm theoBài 5: Làm quen với lập trình CSDL ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
Bài 5: Làm quen với lập trình CSDL ASP.NET - Giáo trình FPT - Có ví dụ kèm theoMasterCode.vn
 
Hdth09 ltudql02-linq-e3
Hdth09 ltudql02-linq-e3Hdth09 ltudql02-linq-e3
Hdth09 ltudql02-linq-e3Dũng Đinh
 
Bài 5: ADO.NET & Kiến trúc không kết nối (tiếp) Điều khiển DataGridView,Combo...
Bài 5: ADO.NET & Kiến trúc không kết nối (tiếp) Điều khiển DataGridView,Combo...Bài 5: ADO.NET & Kiến trúc không kết nối (tiếp) Điều khiển DataGridView,Combo...
Bài 5: ADO.NET & Kiến trúc không kết nối (tiếp) Điều khiển DataGridView,Combo...MasterCode.vn
 
6.adapterset
6.adapterset6.adapterset
6.adaptersetDao Uit
 
Chuong 07_ gắng kết dữ liệu asp.net
Chuong 07_ gắng kết dữ liệu asp.netChuong 07_ gắng kết dữ liệu asp.net
Chuong 07_ gắng kết dữ liệu asp.netThuyNguyenThi25
 
7 databinding-120306231825-phpapp02(1)
7 databinding-120306231825-phpapp02(1)7 databinding-120306231825-phpapp02(1)
7 databinding-120306231825-phpapp02(1)TI Anh
 

Similar to LINQ presentation (20)

Bài 6: Lập trình với CSDL Kiến trúc kết nối & Buộc dữ liệu - Lập trình winfor...
Bài 6: Lập trình với CSDL Kiến trúc kết nối & Buộc dữ liệu - Lập trình winfor...Bài 6: Lập trình với CSDL Kiến trúc kết nối & Buộc dữ liệu - Lập trình winfor...
Bài 6: Lập trình với CSDL Kiến trúc kết nối & Buộc dữ liệu - Lập trình winfor...
 
Aspnet 3.5 _02
Aspnet 3.5 _02Aspnet 3.5 _02
Aspnet 3.5 _02
 
Ung dung web chuong 6
Ung dung web  chuong 6Ung dung web  chuong 6
Ung dung web chuong 6
 
Hdth07 ltudql02-linq-ep1
Hdth07 ltudql02-linq-ep1Hdth07 ltudql02-linq-ep1
Hdth07 ltudql02-linq-ep1
 
Web Services
Web ServicesWeb Services
Web Services
 
2.gioi thieu co ban ado.net cho nguoi lap trinh c#
2.gioi thieu co ban ado.net cho nguoi lap trinh c#2.gioi thieu co ban ado.net cho nguoi lap trinh c#
2.gioi thieu co ban ado.net cho nguoi lap trinh c#
 
Ung dung web chuong 7
Ung dung web  chuong 7Ung dung web  chuong 7
Ung dung web chuong 7
 
Net06 asp.net applications & state management
Net06 asp.net applications & state managementNet06 asp.net applications & state management
Net06 asp.net applications & state management
 
Bài 5: Làm quen với lập trình CSDL ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
Bài 5: Làm quen với lập trình CSDL ASP.NET - Giáo trình FPT - Có ví dụ kèm theoBài 5: Làm quen với lập trình CSDL ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
Bài 5: Làm quen với lập trình CSDL ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
 
Asp
AspAsp
Asp
 
Hdth09 ltudql02-linq-e3
Hdth09 ltudql02-linq-e3Hdth09 ltudql02-linq-e3
Hdth09 ltudql02-linq-e3
 
Introduction Vs2008 Dot Net35
Introduction Vs2008 Dot Net35Introduction Vs2008 Dot Net35
Introduction Vs2008 Dot Net35
 
Bài 5: ADO.NET & Kiến trúc không kết nối (tiếp) Điều khiển DataGridView,Combo...
Bài 5: ADO.NET & Kiến trúc không kết nối (tiếp) Điều khiển DataGridView,Combo...Bài 5: ADO.NET & Kiến trúc không kết nối (tiếp) Điều khiển DataGridView,Combo...
Bài 5: ADO.NET & Kiến trúc không kết nối (tiếp) Điều khiển DataGridView,Combo...
 
Flutter vs React Native 2018
Flutter vs React Native 2018Flutter vs React Native 2018
Flutter vs React Native 2018
 
Ung dung web chuong 5
Ung dung web  chuong 5Ung dung web  chuong 5
Ung dung web chuong 5
 
6.adapterset
6.adapterset6.adapterset
6.adapterset
 
Chuong 07_ gắng kết dữ liệu asp.net
Chuong 07_ gắng kết dữ liệu asp.netChuong 07_ gắng kết dữ liệu asp.net
Chuong 07_ gắng kết dữ liệu asp.net
 
Linq net
Linq net Linq net
Linq net
 
C# co ban 9
C# co ban 9C# co ban 9
C# co ban 9
 
7 databinding-120306231825-phpapp02(1)
7 databinding-120306231825-phpapp02(1)7 databinding-120306231825-phpapp02(1)
7 databinding-120306231825-phpapp02(1)
 

LINQ presentation

  • 1. LINQ TrầnChí Khang Microsoft Student Partners Email: khangtran1990@hotmail.com
  • 2.
  • 11.
  • 13. File  (Access, Excel …)Cách giao tiếp ?
  • 14.
  • 15.
  • 16. 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<T> Without LINQ With LINQ List<Students> MaleStudents = new List<Students>(); Foreach(Students student in Students) { if(student.sex == “male”) { MaleStudents.Add(student ); } } IEnumerable <Students> MaleStudents = from student in Students where student.sex==“male” select student;
  • 17. LINQ TO XML XmlDocumentdoc = newXmlDocument(); XmlElementbooks = doc.CreateElement("books"); XmlElement author = doc.CreateElement("author"); author.InnerText = "Fabrice Marguerie"; XmlElement price= doc.CreateElement(“price"); author2.InnerText = “80”; XmlElement publishyear = doc.CreateElement(“publishyear"); author3.InnerText = “2008"; XmlElementtitle = doc.CreateElement("title"); title.InnerText = “ABC"; XmlElementbook = doc.CreateElement("book"); book.AppendChild(author1); book.AppendChild(author2); book.AppendChild(author3); book.AppendChild(title); books.AppendChild(book); doc.AppendChild(books); XDocument xmlbooks = newXDocument( newXDeclaration("1.0", "UTF-16", "yes"), newXElement("books", newXElement("book", newXAttribute("id","B01"), newXElement("author", "Fabrice Marguerie"), newXElement("price", 80), newXElement("publishyear", 2008), newXElement("title", "ABC")))); <?xmlversion="1.0"encoding="utf-8" ?> <books> <book id = “B01”> <title>ABC</title> <author>Fabrice Marguerie </author> <price>80</price> <pulishyear>2008</pulishyear> </book> </books>
  • 18. LINQ TO XML Cung cấp 1 công cụ mạnh mẽ trong việc truy vấnXML var books = from b in xmlbooks.Elements("book") where(String)b.Element("price") == 80 selectnew { title = (String)b.Element("tilte"), author = (String)b.Element("author") }; foreach (var book in books) { Console.WriteLine(book); }
  • 19. LINQ TO DATASET DATASET ? LINQ to DataSet giúp truy vấn đối tượng Dataset dễ dàng và nhanh chóng var cus = from c in customersDataset.Tables["Customers"].AsEnumerable() select c["CustomerID"]; foreach (var c in cus) { Console.WriteLine("{0}", c); }
  • 20. LINQ TO SQL LINQ to SQL là một phiên bản Object-Relational Mapping (ORM). Database DataContext Table Class View Class Column Field / Property Relationship Field / Property Stored Procedure Method
  • 21.
  • 22. Chuyển câu truy vấn thành câu lệnh SQL
  • 23. Đảm nhận việc tương tác với CSDL
  • 24.
  • 25. CẤU TRÚC LINQ TO SQL from c in db.Customers where c.City == "London" select new {Name, Phone}; db.Customers.InsertOnSubmit(c1); c2.City = “Seattle"; db.Customers.DeleteOnSubmit(c3); SELECT CompanyName, Phone FROM Customers WHERE City == 'London' INSERT INTO Customers… UPDATE Customers…DELETE FROM Customers…
  • 26. Transaction Transactionlà một dịch vụ nhằm đảm bảo rằng một tập các thao tác độc lập sẽ được thực thi như một đơn vị duy nhất NorthwindDataContext db = newNorthwindDataContext(); using(TransactionScope ts = newTransactionScope()) { try { Productprod1 = db.Products.First(p => p.ProductID == 4); Productprod2 = db.Products.First(p => p.ProductID == 5); prod1.UnitsInStock -= 3; prod2.UnitsInStock -= 5; db.SubmitChanges(); ts.Complete(); } catch(Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Error submitting changes,all changes rolled back."); } }
  • 27. DeferredExecution var q = from d in db.Doctors whered.City == "London" selectd; if(orderByLocation) { q = from d in q orderbyd.Country, d.City selectd; } elseif (orderByName) { q = from d in q orderby d.DoctorName select d; } foreach(Doctor d in q) Console.WriteLine(d.DoctorName);
  • 29. TruycậpCSDLvới ADO.NET SqlConnection connection = new SqlConnection(…); connection.Open(); SqlCommandcmd = new SqlCommand( “Select Name, Phone From Customers Where City = @city “); cmd.Parameters.AddWithValue(“@city”, ”London”); Datareaderdr = connection.Execute(cmd); While (dr.Read()) { string name = dr.GetString(0); string phone = dr.GetString(1); DateTime date = dr.GetDateTime(2); } dr.Close(); Connection.Close(); Chuỗi truy vấn Xácđịnhkiểu dữliệukếtquả
  • 32. LINQvsADO.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
  • 33. DEMO

Editor's Notes

  1. Key Message:
  2. Key Message:
  3. Key Message:
  4. Key Message:
  5. Key Message: