The document discusses the LINQ (Language Integrated Query) project in .NET, which allows querying of objects, relational data, and XML in a unified manner. LINQ introduces standard query operators and language features in C# 3.0 and VB 9.0 that enable querying of any .NET collection. It also discusses DLinq for querying relational databases and mapping results to objects, and XLinq for querying XML in a functional, element-centric manner.
4. C# 3.0 Language Innovations 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
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. Local Variable Type Inference int i = 5; string s = "Hello"; double d = 1.0; int[] numbers = new int[] {1, 2, 3}; Dictionary<int,Order> orders = new Dictionary<int,Order>(); var i = 5; var s = "Hello"; var d = 1.0; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary<int,Order>(); “ var” means same type as initializer
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.
10.
11.
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. Deferred Query Execution Customer[] custs = SampleData.GetCustomers(); var query = from c in custs where c.City == "London" select c.Name; var query = custs.Where(c => c.City == "London").Select(c => c.Name); string[] names = query.ToArray(); custs Phone Name ID Select c => c.Name names c => c.City == "London" Where
14. DLinq For Relational Data SqlConnection c = new SqlConnection(…); c.Open(); SqlCommand cmd = new SqlCommand( @"SELECT c.Name, c.Phone FROM Customers c WHERE c.City = @p0"); cmd.Parameters.AddWithValue("@p0", "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. 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 == "London" 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.
17. XLinq For XML Data XmlDocument doc = new XmlDocument(); XmlElement contacts = doc.CreateElement("contacts"); foreach (Customer c in customers) if (c.Country == "USA") { XmlElement e = doc.CreateElement("contact"); XmlElement name = doc.CreateElement("name"); name.InnerText = c.CompanyName; e.AppendChild(name); XmlElement phone = doc.CreateElement("phone"); 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. XLinq For XML Data XElement contacts = new XElement("contacts", from c in customers where c.Country == "USA" select new XElement("contact", new XElement("name", c.CompanyName), new XElement("phone", c.Phone) ) ); Programming XML with XLinq Declarative model Element centric Integrated queries Smaller and faster