Linq 090701233237 Phpapp01

547 views

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
547
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Linq 090701233237 Phpapp01

  1. 1. The LINQ Project 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
  2. 2. Language Integrated Query
  3. 3. How does it work? <ul><li>Standard query operators </li></ul><ul><li>Query expressions </li></ul><ul><li>Extension methods </li></ul><ul><li>Lambda expressions </li></ul><ul><li>Expression trees </li></ul><ul><li>Local variable type inference </li></ul>
  4. 4. C# 3.0 Language Innovations var contacts = from c in customers where c.State == &quot;WA&quot; select new { c.Name, c.Phone }; var contacts = customers .Where(c => c.State == &quot;WA&quot;) .Select(c => new { c.Name, c.Phone }); Extension methods Lambda expressions Query expressions Object initializers Anonymous types Local variable type inference
  5. 5. Lambda Expressions and Extension Methods
  6. 6. Object Initializers public class Rectangle { private Point p1 = new Point(); private Point p2 = new Point(); public Point P1 { get { return p1; } } public Point P2 { get { return p2; } } } Rectangle r = new Rectangle { P1 = { X = 0, Y = 1 }, P2 = { X = 2, Y = 3 } }; Rectangle r = new Rectangle(); r.P1.X = 0; r.P1.Y = 1; r.P2.X = 2; r.P2.Y = 3; Embedded objects No “new Point” Read-only properties
  7. 7. Local Variable Type Inference int i = 5; string s = &quot;Hello&quot;; double d = 1.0; int[] numbers = new int[] {1, 2, 3}; Dictionary<int,Order> orders = new Dictionary<int,Order>(); var i = 5; var s = &quot;Hello&quot;; var d = 1.0; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary<int,Order>(); “ var” means same type as initializer
  8. 8. Anonymous Types public class Customer { public string Name; public Address Address; public string Phone; public List<Order> Orders; … } public class Contact { public string Name; public string Phone; } Customer c = GetCustomer(…); Contact x = new Contact { Name = c.Name, Phone = c.Phone } ; Customer c = GetCustomer(…); var x = new { c.Name, c.Phone } ; Customer c = GetCustomer(…); var x = new { Name = c.Name, Phone = c.Phone } ; class ??? { public string Name; public string Phone; } Projection style initializer
  9. 9. Query Expressions <ul><li>Language integrated query syntax </li></ul>from id in source { from id in source | where condition } [ orderby ordering , ordering , … ] select expr | group expr by key [ into id query ] Starts with from Zero or more from or where Optional orderby Ends with select or group by Optional into continuation
  10. 10. Query Expressions <ul><li>Queries translate to method invocations </li></ul><ul><ul><li>Where, Select, SelectMany, OrderBy, GroupBy </li></ul></ul>from c in customers where c.State == &quot;WA&quot; select new { c.Name, c.Phone }; customers .Where(c => c.State == &quot;WA&quot;) .Select(c => new { c.Name, c.Phone });
  11. 11. C# 3.0 Language Innovations <ul><li>Lambda expressions </li></ul><ul><li>Extension methods </li></ul><ul><li>Local variable type inference </li></ul><ul><li>Object initializers </li></ul><ul><li>Anonymous types </li></ul><ul><li>Query expressions </li></ul><ul><li>Expression trees </li></ul>var x = 5; static void Dump(this object o); c => c.Name new Point { x = 1, y = 2 } new { c.Name, c.Phone } from … where … select Expression<T>
  12. 12. Standard Query Operators GroupBy Grouping Any, All Quantifiers ToArray, ToList, ToDictionary Conversion Count, Sum, Min, Max, Average Aggregation First, FirstOrDefault, ElementAt Elements OfType<T> Casting Distinct, Union, Intersect, Except Sets Take, Skip, TakeWhile, SkipWhile Partitioning OrderBy, ThenBy Ordering Select, SelectMany Projection Where Restriction
  13. 13. Deferred Query Execution Customer[] custs = SampleData.GetCustomers(); var query = from c in custs where c.City == &quot;London&quot; select c.Name; var query = custs.Where(c => c.City == &quot;London&quot;).Select(c => c.Name); string[] names = query.ToArray(); custs Phone Name ID Select c => c.Name names c => c.City == &quot;London&quot; Where
  14. 14. DLinq For Relational Data 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
  15. 15. DLinq For Relational Data 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
  16. 16. DLinq For Relational Data <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>
  17. 17. XLinq For XML Data 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
  18. 18. XLinq For XML Data 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
  19. 19. XLinq For XML Data <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>
  20. 20. The LINQ Project <ul><li>Language Integrated Query for .NET </li></ul><ul><ul><li>Native query syntax in C# 3.0 and VB 9.0 </li></ul></ul><ul><li>Standard Query Operators </li></ul><ul><ul><li>SQL-like queries for any .NET collection </li></ul></ul><ul><li>DLinq </li></ul><ul><ul><li>Query enabled data access framework </li></ul></ul><ul><li>XLinq </li></ul><ul><ul><li>Query enabled, smaller, faster XML DOM </li></ul></ul>
  21. 21. 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>

×