linq language integrated query
play

LINQ: Language Integrated Query ST Colloquium, 2008-05-15 Tom - PowerPoint PPT Presentation

LINQ: Language Integrated Query ST Colloquium, 2008-05-15 Tom Lokhorst Brief example Brief example int[] nrs = {2, 3, 9, 1, 21, 3, 42}; Brief example int[] nrs = {2, 3, 9, 1, 21, 3, 42}; var q = from n in nrs where n < 5 select n * n;


  1. LINQ: Language Integrated Query ST Colloquium, 2008-05-15 Tom Lokhorst

  2. Brief example

  3. Brief example int[] nrs = {2, 3, 9, 1, 21, 3, 42};

  4. Brief example int[] nrs = {2, 3, 9, 1, 21, 3, 42}; var q = from n in nrs where n < 5 select n * n;

  5. Brief example int[] nrs = {2, 3, 9, 1, 21, 3, 42}; var q = from n in nrs where n < 5 select n * n; foreach (var i in q) { Console.WriteLine(i); }

  6. Brief example int[] nrs = {2, 3, 9, 1, 21, 3, 42}; var q = from n in nrs where n < 5 LINQ select n * n; foreach (var i in q) { Console.WriteLine(i); }

  7. Overview The problem space LINQ in .NET 3.5 Deconstructing expressions Usages of LINQ Comparisons with other stuff

  8. Circles, Triangles and Rectangles

  9. Circles, Triangles and Rectangles

  10. Circles, Triangles and Rectangles Business software deals with lots of different types of data: Objects Tree structures (XML) Relational

  11. Assignment A web server should show a list of the top 5 memory-intensive processes. Per process, show its name, and a description. There is a SQL database with descriptions for programs. The list should be in RSS format.

  12. A bit of History

  13. A bit of History .NET 1.0 released early 2002 CLR 1.0, C# 1.0, VB 7 .0 Delegates: managed function pointers

  14. A bit of History .NET 1.0 released early 2002 CLR 1.0, C# 1.0, VB 7 .0 Delegates: managed function pointers .NET 2.0 released late 2005 CLR 2.0, C# 2.0, VB 8.0 Generics, iterators, anonymous delegates

  15. A bit of History .NET 1.0 released early 2002 CLR 1.0, C# 1.0, VB 7 .0 Delegates: managed function pointers .NET 2.0 released late 2005 CLR 2.0, C# 2.0, VB 8.0 Generics, iterators, anonymous delegates .NET 3.5 released late 2007 CLR 2.0, C# 3.0, VB 9.0 Lambda expressions, local type inferencing, extension methods, LINQ

  16. LINQ in .NET 3.5 Libraries LINQ to Objects LINQ to XML LINQ to SQL Language enhancements Lambda expressions Query syntax Compiler enhancements Expression trees

  17. LINQ to Objects

  18. Eating the sugar C# int[] nrs = {2, 3, 9, 1, 21, 3, 42}; 3.0 var q = from n in nrs where n < 5 select n * n;

  19. Eating the sugar C# int[] nrs = {2, 3, 9, 1, 21, 3, 42}; 3.0 IEnumerable<int> q = from n in nrs where n < 5 select n * n;

  20. Eating the sugar C# int[] nrs = {2, 3, 9, 1, 21, 3, 42}; 3.0 IEnumerable<int> q = nrs.Where<int>(n => n < 5) .Select<int, int>(n => n * n);

  21. Eating the sugar C# int[] nrs = {2, 3, 9, 1, 21, 3, 42}; 3.0 IEnumerable<float> q = nrs.Where<int>(n => n < 5) .Select<int, float>(n => n * 0.5F);

  22. Eating the sugar C# int[] nrs = {2, 3, 9, 1, 21, 3, 42}; 3.0 IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, n => n < 5), (n => n * 0.5F));

  23. Eating the sugar C# int[] nrs = {2, 3, 9, 1, 21, 3, 42}; 3.0 IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, n => n < 5), (n => n * 0.5F)); public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate) { ... }

  24. Eating the sugar C# int[] nrs = {2, 3, 9, 1, 21, 3, 42}; 3.0 IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, n => n < 5), (n => n * 0.5F)); public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate) { foreach (var elem in source) if (predicate(elem)) yield return elem; }

  25. Eating the sugar C# int[] nrs = {2, 3, 9, 1, 21, 3, 42}; 3.0 IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, n => n < 5), (n => n * 0.5F));

  26. Eating the sugar C# int[] nrs = {2, 3, 9, 1, 21, 3, 42}; 2.0 IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, delegate(int n){ return n < 5; }), delegate(int n){ return n * 0.5F;} );

  27. Eating the sugar C# int[] nrs = {2, 3, 9, 1, 21, 3, 42}; 1.0* IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, new Func<int, bool>(LessThanFive)), new Func<int, float>(TimesHalf) ); bool LessThanFive(int n) { return n < 5; } float TimesHalf(int n) { return n * 0.5F; }

  28. Eating the sugar C# int[] nrs = {2, 3, 9, 1, 21, 3, 42}; 3.0 var q = from n in nrs where n < 5 select n * n;

  29. LINQ to Objects Query comprehenssions More syntax; orderby, groupby, join Works on you data structures IEnumerable<T> Implement you own Where<T>, Select<T> More library functions

  30. LINQ to XML

  31. Dealing with angle brackets LINQ to XML is a replacement for the W3C Document Object Model, because: It’ s old It’ s ugly It’ s impractical New API, all library: System.Xml.LINQ

  32. Document Object Model Imperative API Document-centric Weird data types

  33. Document Object Model Imperative API Document-centric Weird data types <Company name=”Microsoft”> <Founders> <Person>Bill Gates</Person> </Founders> </Company>

  34. Document Object Model XmlDocument doc = new XmlDocument(); XmlElement company = doc.CreateElement("Company"); doc.AppendChild(company); XmlAttribute name = doc.CreateAttribute("name"); name.Value = "Microsoft"; company.Attributes.Append(name); XmlElement founders = doc.CreateElement("Founders"); company.AppendChild(founders); XmlElement person = doc.CreateElement("Person"); founders.AppendChild(person); XmlText bill = doc.CreateTextNode("Bill Gates"); person.AppendChild(bill);

  35. LINQ to XML Functional construction Element-centric (context free) CLR data types (collection friendly)

  36. LINQ to XML Functional construction Element-centric (context free) CLR data types (collection friendly) XElement company = new XElement("Company", new XAttribute("name", "Microsoft"), new XElement("Founders", new XElement("Person", "Bill Gates") ) );

  37. XML with queries

  38. XML with queries string[] names = { “Anders”, “Erik”, “Amanda” };

  39. XML with queries string[] names = { “Anders”, “Erik”, “Amanda” }; from n in names where n.StartsWith(“A”) select new XElement(“Person”, n)

  40. XML with queries string[] names = { “Anders”, “Erik”, “Amanda” }; XElement employees = new XElement(“Employees”, from n in names where n.StartsWith(“A”) select new XElement(“Person”, n) );

  41. XML with queries string[] names = { “Anders”, “Erik”, “Amanda” }; XElement employees = new XElement(“Employees”, from n in names where n.StartsWith(“A”) select new XElement(“Person”, n) ); company.Add(employees);

  42. XML with queries string[] names = { “Anders”, “Erik”, “Amanda” }; XElement employees = new XElement(“Employees”, from n in names where n.StartsWith(“A”) select new XElement(“Person”, n) ); company.Add(employees); Console.WriteLine(company);

  43. XML on the Console <Company name=”Microsoft”> <Founders> <Person>Bill Gates</Person> </Founders> <Employees> <Person>Anders</Person> <Person>Amanda</Person> </Employees> </Company>

  44. Querying XML IEnumerable<string> persons = from p in company.Descendants(“Person”) select p.Value;

  45. Querying XML IEnumerable<string> persons = from p in company.Descendants(“Person”) select p.Value; Bill Gates Anders Amanda

  46. LINQ to SQL

  47. Querying a SQL backend Use LINQ to query a relational database Strongly typed queries Remote the query to the DBMS C# must remain API/DB independent

  48. LINQ to SQL Simple Object Relational Mapper Microsoft SQL Server Comes with tool to make classes out of tables in an existing database Certainly not the best ORM out there More advanced stuff: LINQ to Entities

  49. LINQ to SQL example

  50. LINQ to SQL example

  51. LINQ to SQL example

  52. LINQ to SQL example [DatabaseAttribute(Name="Northwind")] public partial class NorthwindDataContext : System.Data.LINQ.DataContext { public NorthwindDataContext() : base("Data Source=.;Initial Catalog=Northwind;" + "Integrated Security=True") { } public Table<Customer> Customers { get { return this.GetTable<Customer>(); } } }

  53. LINQ to SQL example [Table(Name="dbo.Customers")] public partial class Customer : INotifyPropertyChanging, INotifyPropertyChanged { private string _CustomerID; [Column(Storage="_CustomerID", DbType="NChar(5) NOT NULL", CanBeNull=false, IsPrimaryKey=true)] public string CustomerID { get { return this._CustomerID; } set { /* Setter code removed */ } } /* More code be here */ }

  54. LINQ to SQL example

  55. LINQ to SQL example var db = new NorthwindDataContext(); var q = from c in db.Customers where c.City == “London” select c;

Recommend


More recommend