Presentation is loading. Please wait.

Presentation is loading. Please wait.

LINQ and C# 3.0 Mads Torgersen Program Manager for the C# Language Microsoft Corporation.

Similar presentations


Presentation on theme: "LINQ and C# 3.0 Mads Torgersen Program Manager for the C# Language Microsoft Corporation."— Presentation transcript:

1 LINQ and C# 3.0 Mads Torgersen Program Manager for the C# Language Microsoft Corporation

2 from c in db.Customers where c.City == “London” select new { c.Name, c.Address }

3 LINQ Architecture Objects XML Relational LINQ enabled data sources LINQ To Objects LINQ To XML LINQ enabled ADO.NET VBOthers… LINQ To Entities LINQ To SQL LINQ To Datasets.Net Language Integrated Query (LINQ) C#

4 C# 3.0 Language Extensions var contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone }; var contacts = customers.Where(c => c.State == "WA").Select(c => new { c.Name, c.Phone }); Extension methods Lambda expressions Query expressions Object initializers Anonymous types Local variable type inference

5 Language Integrated Query

6 Querying in Visual C# 3.0 Extension methods – call static as instance Lambda expressions – inline methods Query expressions – querying made simple Implicitly typed locals – save the typing Anonymous types – temporary results

7 Standard Query Operators Restrict s.Where(…) Project s.Select(…), s.SelectMany(…) Order s.OrderBy(…).ThenBy(…) … Group s.GroupBy(…) Quantify s.Any(…), s.All(…) Partition s.TakeFirst(…), s.SkipFirst(…) Set s.Distinct(), s.Union(…), s.Intersect(…), s.Except(…) Singleton s.Element(…), s.ElementAt(…) Aggregate s.Count(), s.Sum(), s.Min(), s.Max(), s.Average(), s.Aggregate() Convert s.ToArray(), s.ToList() Cast s.OfType (), s.Cast s.OfType (), s.Cast

8 DLinq for Relational Data SqlConnection c = new SqlConnection(…); c.Open(); SqlCommand cmd = new SqlCommand( @"SELECT c.Name, c.Phone @"SELECT c.Name, c.Phone FROM Customers c FROM Customers c WHERE c.City = @p0"); WHERE c.City = @p0"); cmd.Parameters["@p0"] = "London"; DataReader dr = c.Execute(cmd); while (dr.Read()) { string name = r.GetString(0); string name = r.GetString(0); string phone = r.GetString(1); string phone = r.GetString(1); DateTime date = r.GetDateTime(2); DateTime date = r.GetDateTime(2);}r.Close(); Accessing data today Queries in quotes Loosely bound arguments Loosely typed result sets No compile time checks

9 public class Customer { … } public class Northwind: DataContext { public Table Customers; public Table Customers; …} Northwind db = new Northwind(…); var contacts = from c in db.Customers from c in db.Customers where c.City == "London" where c.City == "London" select new { c.Name, c.Phone }; select new { c.Name, c.Phone }; DLinq for Relational Data Accessing data with DLinq Classes describe data Strongly typed connection Integrated query syntax Strongly typed results Tables are like collections

10 XLinq for XML Data XmlDocument doc = new XmlDocument(); XmlElement contacts = doc.CreateElement("contacts"); foreach (Customer c in customers) if (c.Country == "USA") { if (c.Country == "USA") { XmlElement e = doc.CreateElement("contact"); XmlElement e = doc.CreateElement("contact"); XmlElement name = doc.CreateElement("name"); XmlElement name = doc.CreateElement("name"); name.InnerText = c.CompanyName; name.InnerText = c.CompanyName; e.AppendChild(name); e.AppendChild(name); XmlElement phone = doc.CreateElement("phone"); XmlElement phone = doc.CreateElement("phone"); phone.InnerText = c.Phone; phone.InnerText = c.Phone; e.AppendChild(phone); e.AppendChild(phone); contacts.AppendChild(e); contacts.AppendChild(e); }doc.AppendChild(contacts); Programming XML today <contacts> Great Lakes Food Great Lakes Food (503) 555-7123 (503) 555-7123 …</contacts> Imperative model Document centric No integrated queries Memory intensive

11 XLinq for XML Data XElement contacts = new XElement("contacts", from c in customers from c in customers where c.Country == "USA" where c.Country == "USA" select new XElement("contact", select new XElement("contact", new XElement("name", c.CompanyName), new XElement("name", c.CompanyName), new XElement("phone", c.Phone) new XElement("phone", c.Phone) )); Programming XML with XLinq Declarative model Element centric Integrated queries Smaller and faster

12 LINQ Extensibility New data sources Implement the LINQ pattern New languages Unified querying experience Expression tree generation Unified call syntax Query syntax

13 Try it! http://msdn.microsoft.com/netframework/future/linq/

14 © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.


Download ppt "LINQ and C# 3.0 Mads Torgersen Program Manager for the C# Language Microsoft Corporation."

Similar presentations


Ads by Google