260 likes | 429 Views
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
E N D
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 • Queries without LINQ • Key features of LINQ • LINQ Architecture • LINQ to…
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
What is LINQ? • Language Integrated Query • Make query a part of the language • Component of .NET Framework 3.5
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
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
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.
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)
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
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
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.
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.
Summary • Had a quick intro on • LINQ and its need • Key Language features • LINQ Architecture • LINQ Types • LINQ Operators
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/