Visual C#: Directions in Language Innovation

Download Report

Transcript Visual C#: Directions in Language Innovation

LINQ and C# 3.0
Mads Torgersen
Program Manager for the C# Language
Microsoft Corporation
from c in db.Customers
where c.City == “London”
select new { c.Name, c.Address }
LINQ Architecture
C#
Others…
VB
.Net Language Integrated Query (LINQ)
LINQ enabled data sources
LINQ enabled ADO.NET
LINQ
To Objects
LINQ
To Datasets
LINQ
To SQL
LINQ
To Entities
LINQ
To XML
<book>
<title/>
<author/>
<price/>
</book>
Objects
Relational
XML
C# 3.0 Language Extensions
Local variable
type inference
Query
var contacts =
expressions
from c in customers
where c.State == "WA"
select new { c.Name, c.Phone };
Lambda
expressions
var contacts =
customers
.Where(c => c.State == "WA")
.Select(c => new { c.Name, c.Phone });
Extension
methods
Anonymous
types
Object
initializers
Language Integrated Query
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
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<T>(), s.Cast<T>
DLinq for Relational Data
Accessing data today
SqlConnection c = new SqlConnection(…);
Queries in
c.Open();
quotes
SqlCommand cmd = new SqlCommand(
@"SELECT c.Name, c.Phone
FROM Customers c
Loosely bound
WHERE c.City = @p0");
arguments
cmd.Parameters["@p0"] = "London";
DataReader dr = c.Execute(cmd);
while (dr.Read()) {
Loosely typed
result sets
string name = r.GetString(0);
string phone = r.GetString(1);
DateTime date = r.GetDateTime(2);
}
r.Close();
No compile time
checks
DLinq for Relational Data
Accessing data with DLinq
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 };
Classes
describe data
Tables are like
collections
Strongly typed
connection
Integrated
query syntax
Strongly typed
results
XLinq for XML Data
Programming XML today
Imperative
model
XmlDocument doc = new XmlDocument();
XmlElement contacts = doc.CreateElement("contacts");
Document
foreach (Customer c in customers)
centric
if (c.Country == "USA") {
XmlElement e = doc.CreateElement("contact");
No integrated
XmlElement name = doc.CreateElement("name");
queries
name.InnerText = c.CompanyName;
e.AppendChild(name);
XmlElement phone = doc.CreateElement("phone");
Memory
phone.InnerText = c.Phone;
intensive
e.AppendChild(phone);
<contacts>
<contact>
contacts.AppendChild(e);
<name>Great Lakes Food</name>
}
<phone>(503) 555-7123</phone>
doc.AppendChild(contacts);
</contact>
…
</contacts>
XLinq for XML Data
Programming XML with XLinq
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)
)
);
Declarative
model
Element
centric
Integrated
queries
Smaller and
faster
LINQ Extensibility
New data sources
Implement the LINQ pattern
New languages
Unified querying experience
Expression tree generation
Unified call syntax
Query syntax
Try it!
http://msdn.microsoft.com/netframework/future/linq/
© 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.