Re Descubriendo A Linq

1,620 views

Published on

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

No Downloads
Views
Total views
1,620
On SlideShare
0
From Embeds
0
Number of Embeds
103
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Re Descubriendo A Linq

  1. 1. Re-descubriendo LINQ Jose Fco Bonnin Payvision Blog: http://www.josefcobonnin.com
  2. 2. Agenda <ul><li>Historia </li></ul><ul><li>LINQ Basics </li></ul><ul><li>LINQ to XML </li></ul><ul><li>LINQ to SQL </li></ul>
  3. 3. LINQ Overview C# 3.0 VB 9.0 Otros… LINQ to SQL LINQ to XML LINQ to DataSets .NET Language Integrated Query LINQ to Objects LINQ to Entities LINQ to Whatever Objetos Datos relacionales <book> <title/> <author/> <year/> <price/> </book> XML
  4. 4. Demo <ul><li>LINQ </li></ul>
  5. 5. Historia <ul><li>OR/M </li></ul><ul><ul><li>Finales 2000 - Cheops </li></ul></ul><ul><ul><li>2001 PDC – Presentación ObjectSpaces </li></ul></ul><ul><li>Evolución Lenguaje </li></ul><ul><ul><li>2001 BillG Thinkweek – XML Types for C# </li></ul></ul><ul><ul><li>2002 XML Conference - X# </li></ul></ul><ul><ul><li>2002 Microsoft Research – Polyphonic C# </li></ul></ul><ul><ul><li>2003 XML Conference - Xen </li></ul></ul><ul><ul><li>2004 Microsoft Research – C ω </li></ul></ul><ul><li>2005 PDC - Presentación oficial de LINQ </li></ul>
  6. 6. My First Query <ul><li>int[] numbers = { 5, 10, 8, 3, 6, 12 }; </li></ul><ul><li>IEnumerable<int> myFirstQuery = Enumerable.Where(numbers, num => num > 6); </li></ul><ul><li>myFirstQuery = </li></ul><ul><li>Enumerable.OrderBy(myFirstQuery, n => n); </li></ul><ul><li>foreach (int i in myFirstQuery) </li></ul><ul><li>{ </li></ul><ul><li>Console.WriteLine(i); </li></ul><ul><li>} </li></ul>
  7. 7. Standard Query Operators I Agrupados por tipo de Operación <ul><li>Sorting </li></ul><ul><ul><li>OrderBy </li></ul></ul><ul><ul><li>ThenBy </li></ul></ul><ul><ul><li>OrderByDescending </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><li>Aggregation </li></ul><ul><ul><li>Aggregate </li></ul></ul><ul><ul><li>Average </li></ul></ul><ul><ul><li>Count </li></ul></ul><ul><ul><li>LongCount </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><li>Grouping </li></ul><ul><ul><li>GroupBy </li></ul></ul><ul><ul><li>ToLookUp </li></ul></ul><ul><li>Projection </li></ul><ul><ul><li>Select </li></ul></ul><ul><ul><li>SelectMany </li></ul></ul><ul><li>… </li></ul>
  8. 8. Standard Query Operators II Agrupados por tipo de Ejecución <ul><li>Immediate </li></ul><ul><ul><li>Aggregate </li></ul></ul><ul><ul><li>Any </li></ul></ul><ul><ul><li>Average </li></ul></ul><ul><ul><li>First </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><li>Deferred - Streaming </li></ul><ul><ul><li>Cast </li></ul></ul><ul><ul><li>Distinct </li></ul></ul><ul><ul><li>Select </li></ul></ul><ul><ul><li>Where </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><li>Deferred – Non Streaming </li></ul><ul><ul><li>GroupBy </li></ul></ul><ul><ul><li>Join </li></ul></ul><ul><ul><li>OrderBy </li></ul></ul><ul><ul><li>… </li></ul></ul>
  9. 9. Demo <ul><li>Immediate and Deferred Execution </li></ul>
  10. 10. Query Expressions Syntax I <ul><li>from-clause: </li></ul><ul><li>from [Type] identifier in expression [join clauses] </li></ul><ul><li>i.e from num in new int[] {1, 2, 3, 4} </li></ul><ul><li>  </li></ul><ul><li>join clause: </li></ul><ul><li>join [Type] identifier in expression on expression equals expression </li></ul>
  11. 11. Query Expressions Syntax II
  12. 12. Query Expression <ul><li>var a = from p in people </li></ul><ul><ul><ul><ul><li>where p.Age > 15 </li></ul></ul></ul></ul><ul><li> orderby p.Name, p.Age descending </li></ul><ul><li> select new { p.Name, p.Surname}; </li></ul>Implicitly Typed Variables Extension Methods Lambda Expression Object Initializer Anonymous Type var a = people.Where(p => p.Age > 15) .OrderBy (p => p.Name) .ThenByDescending(p => p.Age) .Select(p => new p.Name, p.Surname);
  13. 13. Demo <ul><li>Query Expressions </li></ul>
  14. 14. “ PRE” LINQ to XML – W3C DOM XML <ul><li>XmlDocument xmlDoc = new XmlDocument(); </li></ul><ul><li>XmlElement freaks = xmlDoc.CreateElement(&quot;Freaks&quot;); </li></ul><ul><li>xmlDoc.AppendChild(freaks); </li></ul><ul><li>XmlElement freak = xmlDoc.CreateElement(&quot;Freak&quot;); </li></ul><ul><li>XmlAttribute freakType = xmlDoc.CreateAttribute(&quot;Speciality&quot;); </li></ul><ul><li>freakType.InnerText = “TFS&quot;; </li></ul><ul><li>freak.Attributes.Append(freakType); </li></ul><ul><li>XmlElement name = xmlDoc.CreateElement(&quot;Name&quot;); </li></ul><ul><li>name.InnerText = &quot;Luis&quot;; </li></ul><ul><li>freak.AppendChild(name); </li></ul><ul><li>XmlElement surname = xmlDoc.CreateElement(&quot;Surname&quot;); </li></ul><ul><li>surname.InnerText = &quot;Fraile&quot;; </li></ul><ul><li>freak.AppendChild(surname); </li></ul><Freaks> <Freak Specialty=&quot;TFS&quot;> <Name>Luis</Name> <Surname>Fraile</Surname> </Freak> </Freaks>
  15. 15. “ PRE” LINQ to XML – XML “Lite” <ul><li>string xml = </li></ul><ul><li>“ <Freaks> </li></ul><ul><li><Freak Specialty=&quot;{0}&quot;><Name>{1}</Name><Surname>{2}</Surname></Freak> </li></ul><ul><li></Freaks>”; </li></ul><ul><li>string formattedXml = string.Format(xml, &quot;TFS&quot;, &quot;Luis&quot;, &quot;Fraile&quot;); </li></ul>
  16. 16. LINQ to XML
  17. 17. Demo <ul><li>LINQ to XML </li></ul>
  18. 18. Expression Trees <ul><li>Expression<Func<int,int,int>> exp = (a,b) => a + b </li></ul><ul><li>ParameterExpression left = Expression.Parameter(typeof(int), &quot;a&quot;); </li></ul><ul><li>ParameterExpression right= Expression.Parameter(typeof(int), &quot;b&quot;); </li></ul><ul><li>BinaryExpression body = Expression.Add(left, right); </li></ul><ul><li>Expression expression = Expression.Lambda(body, left, right); </li></ul>Add a b Left Right Body
  19. 19. Demo <ul><li>LINQ to SQL != Transact/SQL </li></ul>
  20. 20. LINQ to SQL <ul><li>¿Sólo para RAD? </li></ul><ul><li>Service Oriented Architecture, ¿Se puede o no? </li></ul><ul><li>LINQ to SQL o Entity Framework </li></ul>
  21. 21. Demo <ul><li>LINQ to SQL </li></ul>
  22. 22. Fin <ul><li>this.Dispose(); </li></ul>
  23. 23. Referencias <ul><li>C# 3.o Specification </li></ul><ul><li>http://msdn.microsoft.com/en-us/library/ms364047(VS.80).aspx </li></ul><ul><li>The LINQ Project: </li></ul><ul><li>http://msdn.microsoft.com/en-us/netframework/aa904594.aspx </li></ul><ul><li>Standard Query Operators </li></ul><ul><li>http://download.microsoft.com/download/5/8/6/5868081c-68aa-40de-9a45-a3803d8134b8/Standard_Query_Operators.doc </li></ul><ul><li>101 Linq Samples </li></ul><ul><li>http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx </li></ul><ul><li>Jose Blog: </li></ul><ul><li>http://www.josefcobonnin.com </li></ul>

×