1 / 26

Introduction to Language‐Integrated Query (LINQ)

Introduction to Language‐Integrated Query (LINQ). LÊ VĂN PHONG - 11070467. Content. Introduction Using LINQ to SQL Using LINQ to DataSets Using LINQ to XML Conclusion. Content. Introduction Using LINQ to SQL Using LINQ to DataSets Using LINQ to XML Conclusion. Introduction.

Download Presentation

Introduction to Language‐Integrated Query (LINQ)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Language‐Integrated Query (LINQ) LÊ VĂN PHONG - 11070467

  2. Content • Introduction • Using LINQ to SQL • Using LINQ to DataSets • Using LINQ to XML • Conclusion

  3. Content • Introduction • Using LINQ to SQL • Using LINQ to DataSets • Using LINQ to XML • Conclusion

  4. Introduction • LINQ is a set of features introduced in Visual Studio 2008 • It extends powerful query capabilities to the language syntax of C# and Visual Basic • LINQ introduces standard, easily-learned patterns for querying and updating data • Can use LINQ with .NET Framework, SQL Server, ADO.NET, and XML

  5. Content • Introduction • Using LINQ to SQL • Using LINQ to DataSets • Using LINQ to XML • Conclusion

  6. LINQ to SQL SELECT Statement SQL SELECT * FROM Customers WHERE City = "London" LINQ var q = from c in Customers where c.City == "London" select c;

  7. LINQ to SQL • COUNT – SUM – MIN – MAX – AVG • JOIN – ODER BY – GROUP BY – HAVING • INSERT – UPDATE – DELETE • OTHERS

  8. Why LINQ Beats SQL • SQL is a very old language—invented in 1974. Since then it's been extended endlessly, but never redesigned  This has made the language messy • LINQ is the language with syntax of C# and Visual Basic  Simpler, tidier, and higher-level

  9. Why LINQ Beats SQL SQL SELECT UPPER(Name) FROM Customer WHERE Name LIKE 'A%' ORDER BY Name LINQ var query = from c in db.Customers where c.Name.StartsWith ("A") orderbyc.Name select c.Name.ToUpper();

  10. Why LINQ Beats SQL Simpler SQL SELECT TOP 10 UPPER (c1.Name) FROMCustomer c1 WHERE c1.Name LIKE 'A%' AND c1.ID NOT IN ( SELECT TOP 20 c2.ID FROMCustomer c2 WHEREc2.Name LIKE 'A%' ORDER BY c2.Name ) ORDER BY c1.Name LINQ var query = from c in db.Customers where c.Name.StartsWith ("A") orderbyc.Name select c.Name.ToUpper(); var result = query.Skip(20).Take(10);

  11. Why LINQ Beats SQL Associations SQL SELECT p.* FROM Purchase p LEFT OUTER JOIN Customer c INNER JOIN Address a ON c.AddressID = a.ID ON p.CustomerID = c.ID WHERE(a.State = 'WA' || p.CustomerID IS NULL) AND p.ID in ( SELECTPurchaseID FROMPurchaseItem GROUP BY PurchaseID HAVING SUM(SaleAmount) > 1000 ) LINQ from p in db.Purchases where p.Customer.Address.State == "WA" || p.Customer == null where p.PurchaseItems.Sum (pi => pi.SaleAmount) > 1000 select p;

  12. Why LINQ Beats SQL Parameterization LINQ string state = "WA"; varquery = from c in db.Customerswhere c.Address.State== state select c;

  13. Content • Introduction • Using LINQ to SQL • Using LINQ to DataSets • Using LINQ to XML • Conclusion

  14. LINQ To Datasets Restriction var numbers = testDS.Tables["Numbers"].AsEnumerable(); varlowNums = from n in numbers where n.Field<int>("number") < 5 select n;

  15. LINQ To Datasets Projection var products = testDS.Tables["Products"].AsEnumerable(); varproductNames = from p in products select p.Field<string>("ProductName");

  16. LINQ to Datasets • COUNT – SUM – MIN – MAX – AVG • JOIN – ODER BY – GROUP BY • OTHERS

  17. Content • Introduction • Using LINQ to SQL • Using LINQ to DataSets • Using LINQ to XML • Conclusion

  18. LINQ To XML Load XDocumentdoc = XDocument.Load(dataPath + "bib.xml"); string xml = "<book price='100' isbn='1002310'>" + "<title>XClarity Samples</title>" + "<author>Matt</author>" + "</book>"; XDocumentdoc = XDocument.Parse(xml);

  19. LINQ To XML Construction XDocumentmyDocument = new XDocument( new XElement("configuration", new XElement("system.web", new XElement("membership", new XElement("providers", new XElement("add", new XAttribute("name", "WebAdminMembershipProvider"), new XAttribute("type“, "System.Web.Administration.WebAdminMembershipProvider")))))));

  20. LINQ To XML Query XDocument doc = XDocument.Load(dataPath + "nw_customers.xml"); var query = doc.Element("Root") .Element("Customers") .Elements(); foreach (XElement result in query)

  21. LINQ To XML Save StringWritersw = new StringWriter(); //save to XmlWriter XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; XmlWriter w = XmlWriter.Create(sw, settings); doc.Save(w); w.Close(); //save to file doc.Save("out.xml");

  22. Content • Introduction • Using LINQ to SQL • Using LINQ to DataSets • Using LINQ to XML • Conclusion

  23. Conclusion Advantages • LINQ is easily-learned, simpler and tidier than SQL • Don’t need the really database programmers • Can manipulate many type of datasoures • Tables are automatically created into class • Columns are automatically created into properties • Data is easy to setup and use

  24. Conclusion Disadvantages • There is an overhead for creating queries • The programmers are hard to understand how does it work

  25. References [1] http://msdn.microsoft.com/en-us/library/bb397926.aspx [2] http://www.linqpad.net/whylinqbeatssql.aspx

  26. Thank You!

More Related