1 / 20

Introduction to LINQ

Introduction to LINQ. Lecture # 19 August 2 2013. Introduction . How do you interrogate/manipulate data? What if you could do the work in a type-safe," string-free manner? What if you could use a consistent querying syntax for data, objects or XML?. Agenda. What is LINQ

margie
Download Presentation

Introduction to 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 LINQ Lecture # 19 August 2 2013

  2. Introduction • How do you interrogate/manipulate data? • What if you could do the work in a type-safe," string-free manner? • What if you could use a consistent querying syntax for data, objects or XML?

  3. Agenda • What is LINQ • Queries without LINQ • Key features of LINQ • LINQ Architecture • LINQ to…

  4. Introduction • We use many different types of query • SQL, XQuery/XPath, DataView row filters, etc. • Maybe we could enhance productivity by... • Deciding on one query expression syntax • Enabling compilers to check queries & results • Allow extensibility to target all kinds of data

  5. What is LINQ? • Language Integrated Query • Make query a part of the language • Component of .NET Framework 3.5

  6. Objects using loops and conditions foreach(Customer c in customers) if (c.Region == "USA") ... • SELECT from database tables SELECT * FROM Customers WHERE Region='USA' • XML using XPath/XQuery //Customers/Customer[@Region='USA'] Queries without LINQ

  7. Language Innovations Query expressions var contacts = from c in customers where c.City == "Hove" select new { c.Name, c.Phone }; Local variable type inference Lambda expressions var contacts = customers .Where(c => c.City == "Hove") .Select(c => new { c.Name, c.Phone }); Extension methods Object initializers Anonymous types

  8. Key Features of LINQ • Delayed Execution • LINQ queries don't execute until they must • Retrieve specific values • Iterate through the collection • Perform an operation • Write Data Access Code directly • Compile time syntax and schema checking (intellisense too) • no need of inline sql or to wait until runtime to see if it is ok • LINQ data access code abstracted from underlying data • consistent syntax across various data sources • can join information from different sources.

  9. Architecture

  10. LINQ to… • LINQ to Objects • LINQ to SQL (formerly known as DLINQ) • LINQ to XML (formerly known as XLINQ) • LINQ to Entities (ADO.NET Entities)

  11. LINQ to Objects • Query any IEnumerable<T> sourceIncludes arrays, List<T>, Dictionary... • Many useful operators availableSum, Max, Min, Distinct, Intersect, Union • Expose your own data with IEnumerable<T> or IQueryable<T> • Create operators using extension methods

  12. New Language Features – LINQ to objects

  13. LINQ to SQL (formerly known as DLinq) • Object-relational mapping (ORM) • Records become strongly-typed objects. • Includes tracking of changed objects and persistence. • Ensures that you will not obtain multiple objects for the same underlying row in the database ( Fowler’s Identity Map) • Data context is the controller mechanism • Facilitates update, delete & insert • Type, parameter and injection safe

  14. LINQ to SQL

  15. LINQ to XML (formerly known as XLinq) • Similar to LINQ to SQL but along the idea of querying XML documents using LINQ syntax rather than the XPath/XQuery syntax.

  16. LINQ to XML

  17. LINQ to Entities • LINQ to Entities (ADO.NET Entities) • A more advanced ORM solution that allows more extensive mapping and manipulation between how the object appears and the underlying data source. • LINQ to Entity provides • Excellent build support; if it isn't mapped 100%, it won't build • It works with other databases (I'm currently using it with Oracle) • It properly seperates the structural model from the conceptual entity model. • It maps many to many relationships properly.

  18. LINQ to Entities

  19. Summary • Had a quick intro on • LINQ and its need • Key Language features • LINQ Architecture • LINQ Types • LINQ Operators

  20. For more information…. • Official site http://msdn.microsoft.com/en-us/netframework/aa904594.aspx • Tutorials http://weblogs.asp.net/scottgu/archive/tags/LINQ/default.aspx • 101 LINQ Samples http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx • LINQ Pad http://www.linqpad.net/

More Related