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;
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
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);
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