Published on

Introduction to Language Integrated Query

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • LINQ

    1. 1. LINQ, DLINQ and XLINQ Ajeet Kumar C# 3.0
    2. 2. What is LINQ? <ul><li>“ Language Integrated Query” </li></ul><ul><li>Evolution of application data management </li></ul><ul><li>Coming with C# 3.0 and VB.NET 9.0 </li></ul><ul><li>Goals </li></ul><ul><ul><li>Provide overview of the LINQ project </li></ul></ul><ul><ul><li>Point out learning resources for self study </li></ul></ul>
    3. 3. Philosophy of C# 3.0 And LINQ <ul><li>Integrates </li></ul><ul><ul><li>object, </li></ul></ul><ul><ul><li>relational </li></ul></ul><ul><ul><li>semi-structured data models. </li></ul></ul><ul><li>By generalization, rather than by ad-hoc specializations </li></ul><ul><li>Similar to database systems which now integrate </li></ul><ul><ul><li>the relational and XML data model </li></ul></ul><ul><ul><li>SQL and XQuery. </li></ul></ul>
    4. 4. The Problem <ul><li>Worlds of OO and data access are far, far apart </li></ul><ul><li>OO language types and native database types are often different </li></ul><ul><li>SQL code is “inside the quotes” </li></ul><ul><ul><ul><li>No strong typing or compile-time type checking </li></ul></ul></ul><ul><ul><ul><li>No Intellisense </li></ul></ul></ul><ul><ul><ul><li>No Statement completion </li></ul></ul></ul><ul><li>SQL and XML have query languages, objects to not </li></ul>LINQ seeks to bridge the gap
    5. 5. What is LINQ? Standard Query Operators DLinq (ADO.NET) XLinq (System.Xml) .NET Language Integrated Query C# VB Others… Objects <book> <title/> <author/> <year/> <price/> </book> XML SQL WinFS
    6. 6. Standard Query Operators Restriction Where Projection Select, SelectMany Ordering OrderBy, ThenBy Grouping GroupBy Quantifiers Any, All Partitioning Take, Skip, TakeWhile, SkipWhile Sets Distinct, Union, Intersect, Except Elements First, FirstOrDefault, ElementAt Aggregation Count, Sum, Min, Max, Average Conversion ToArray, ToList, ToDictionary Casting OfType<T>
    7. 7. DLinq <ul><li>A means of managing relational data </li></ul>SqlConnection c = new SqlConnection(…); c.Open(); SqlCommand cmd = new SqlCommand( @&quot;SELECT c.Name, c.Phone FROM Customers c WHERE c.City = @p0&quot;); cmd.Parameters.AddWithValue(&quot;@p0&quot;, &quot;London“); DataReader dr = c.Execute(cmd); while (dr.Read()) { string name = dr.GetString(0); string phone = dr.GetString(1); DateTime date = dr.GetDateTime(2); } dr.Close(); Accessing data today: Queries in quotes Loosely bound arguments Loosely typed result sets No compile time checks
    8. 8. DLinq public class Customer { … } public class Northwind: DataContext { public Table<Customer> Customers; … } Northwind db = new Northwind(…); var contacts = from c in db.Customers where c.City == &quot;London&quot; select new { c.Name, c.Phone }; Accessing data with DLinq Classes describe data Strongly typed connection Integrated query syntax Strongly typed results Tables are like collections
    9. 9. DLinq <ul><li>Language integrated data access </li></ul><ul><ul><li>Maps tables and rows to classes and objects </li></ul></ul><ul><ul><li>Builds on ADO.NET and .NET Transactions </li></ul></ul><ul><li>Mapping </li></ul><ul><ul><li>Encoded in attributes </li></ul></ul><ul><ul><li>Relationships map to properties </li></ul></ul><ul><li>Persistence </li></ul><ul><ul><li>Automatic change tracking </li></ul></ul><ul><ul><li>Updates through SQL or stored procedures </li></ul></ul>
    10. 10. XLinq XmlDocument doc = new XmlDocument(); XmlElement contacts = doc.CreateElement(&quot;contacts&quot;); foreach (Customer c in customers) if (c.Country == &quot;USA&quot;) { XmlElement e = doc.CreateElement(&quot;contact&quot;); XmlElement name = doc.CreateElement(&quot;name&quot;); name.InnerText = c.CompanyName; e.AppendChild(name); XmlElement phone = doc.CreateElement(&quot;phone&quot;); phone.InnerText = c.Phone; e.AppendChild(phone); contacts.AppendChild(e); } doc.AppendChild(contacts); Programming XML today <contacts> <contact> <name>Great Lakes Food</name> <phone>(503) 555-7123</phone> </contact> … </contacts> Imperative model Document centric No integrated queries Memory intensive
    11. 11. XLinq XElement contacts = new XElement(&quot;contacts&quot;, from c in customers where c.Country == &quot;USA&quot; select new XElement(&quot;contact&quot;, new XElement(&quot;name&quot;, c.CompanyName), new XElement(&quot;phone&quot;, c.Phone) ) ); Programming XML with XLinq Declarative model Element centric Integrated queries Smaller and faster
    12. 12. XLinq <ul><li>Language integrated query for XML </li></ul><ul><ul><li>Expressive power of XPath / XQuery </li></ul></ul><ul><ul><li>But with C# or VB as programming language </li></ul></ul><ul><li>Leverages experience with DOM </li></ul><ul><ul><li>Element centric, not document centric </li></ul></ul><ul><ul><li>Functional construction </li></ul></ul><ul><ul><li>Text nodes are just strings </li></ul></ul><ul><ul><li>Simplified XML namespace support </li></ul></ul><ul><ul><li>Faster and smaller </li></ul></ul>
    13. 13. What is LINQ? <ul><li>The LINQ project is: </li></ul><ul><ul><li>Language Integrated Query for .NET </li></ul></ul><ul><ul><ul><li>Native query syntax for .NET languages </li></ul></ul></ul><ul><ul><li>Standard Query Operators </li></ul></ul><ul><ul><ul><li>SQL-like method extensions for any .NET collection </li></ul></ul></ul><ul><ul><ul><li>System.Query namespace </li></ul></ul></ul><ul><ul><li>DLinq </li></ul></ul><ul><ul><ul><li>Code name for future version of ADO.NET </li></ul></ul></ul><ul><ul><ul><li>Query enabled data access framework </li></ul></ul></ul><ul><ul><ul><li>System.Data.Xlinq namespace </li></ul></ul></ul><ul><ul><li>XLinq </li></ul></ul><ul><ul><ul><li>Query enabled, smaller, faster XML DOM </li></ul></ul></ul><ul><ul><ul><li>System.XML.Xlinq namespace </li></ul></ul></ul>
    14. 14. Benefits of LINQ <ul><li>Unified querying of objects, relational, XML </li></ul><ul><li>Type checking and IntelliSense for queries </li></ul><ul><li>SQL and XQuery-like power in C# and VB </li></ul><ul><li>Extensibility model for languages / APIs </li></ul>