LINQ: It’s Not Your
Father’s Data Access
Denny Boynton
Anheuser-Busch Companies
Goals
– Provide overview of the LINQ project
– Demonstrate basic functionality of LINQ
– Point out learning resources for self
study
What is LINQ?
“Language Integrated Query”
Evolution of application data
management
Anders Hejlsberg
Coming with C# 3.0 and
VB.NET 9.0
Don Box
The Problem
– Worlds of OO and data access are far,
far apart
– OO language types and native database
types are often different
– SQL code is “inside the quotes”
• No strong typing or compile-time type
checking
• No Intellisense
• No Statement completion
– SQL and XML have query languages,
objects to not
LINQ seeks to bridge the gap
What is LINQ?
C#
VB
Others…
.NET Language Integrated Query
Standard
Query
Operators
DLinq
(ADO.NET)
XLinq
(System.Xml)
<book>
<title/>
<author/>
<year/>
<price/>
</book>
Objects
SQL
WinFS
XML
Source: Anders Hejlsberg’s PDC Presentation
Demo
Examine the basics of LINQ
Language Enhancements
Lambda Expressions
Extension Methods
Local Variable Type Inference
Object Initializers
Anonymous Types
Query Expressions
C =>
var
n=
c.Name
5;
c.Customers.Where(…)
New
from..where…select
c.Phone}
new {c.Name,
Point {x=1,
y=2}
.Select(…)
Standard Query Operators
Restriction
Where
Projection
Select, SelectMany
Ordering
OrderBy, ThenBy
Grouping
GroupBy
Quantifiers
Any, All
Partitioning
Take, Skip, TakeWhile, SkipWhile
Sets
Distinct, Union, Intersect, Except
Elements
First, FirstOrDefault, ElementAt
Aggregation
Count, Sum, Min, Max, Average
Conversion
ToArray, ToList, ToDictionary
Casting
OfType<T>
Source: Anders Hejlsberg’s PDC Presentation
DLinq
A means of managing relational data
Accessing data today:
Queries in
quotes
SqlConnection c = new SqlConnection(…);
c.Open();
SqlCommand cmd = new SqlCommand(
Loosely bound
@"SELECT c.Name, c.Phone
arguments
FROM Customers c
WHERE c.City = @p0");
cmd.Parameters.AddWithValue("@p0", "London“);
DataReader dr = c.Execute(cmd);
Loosely typed
while (dr.Read()) {
result sets
string name = dr.GetString(0);
string phone = dr.GetString(1);
DateTime date = dr.GetDateTime(2);
}
No compile time
dr.Close();
checks
DLinq
Accessing data with DLinq
public class Customer { … }
Classes
describe data
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 };
Tables are
like
collections
Strongly
typed
connection
Integrated
query syntax
Strongly
typed results
DLinq
• Language integrated data access
– Maps tables and rows to classes and objects
– Builds on ADO.NET and .NET Transactions
• Mapping
– Encoded in attributes
– Relationships map to properties
• Persistence
– Automatic change tracking
– Updates through SQL or stored procedures
XLinq
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>
contacts.AppendChild(e); <contact>
<name>Great Lakes
}
Food</name>
doc.AppendChild(contacts);
<phone>(503) 5557123</phone>
</contact>
…
</contacts>
XLinq
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
XLinq
• Language integrated query for XML
– Expressive power of XPath / XQuery
– But with C# or VB as programming
language
• Leverages experience with DOM
– Element centric, not document centric
– Functional construction
– Text nodes are just strings
– Simplified XML namespace support
– Faster and smaller
Demo
Show DLinq and XLinq in action
What is LINQ?
The LINQ project is:
– Language Integrated Query for .NET
• Native query syntax for .NET languages
– Standard Query Operators
• SQL-like method extensions for any .NET collection
• System.Query namespace
– DLinq
• Code name for future version of ADO.NET
• Query enabled data access framework
• System.Data.Xlinq namespace
– XLinq
• Query enabled, smaller, faster XML DOM
• System.XML.Xlinq namespace
Benefits of LINQ
• Unified querying of objects, relational,
XML
• Type checking and IntelliSense for queries
• SQL and XQuery-like power in C# and VB
• Extensibility model for languages / APIs
Closing Thoughts
• Not sure where use of LINQ will
coincide with SQL
• Use may well be driven by data
management standards of an
organization
– Where should data access really “live”
• ORM synchronization
• This could really be a revolution
Resources
LINQ Project Home Page
– http://msdn.microsoft.com/data/ref/linq/
Anders Hejlsberg – LINQ
– http://channel9.msdn.com/showpost.aspx?postid=1146
80
LINQ Project Overview Whitepaper
– http://msdn.microsoft.com/data/ref/linq/default.aspx?p
ull=/library/en-us/dndotnet/html/linqprojectovw.asp
Thank You
Denny Boynton
Email: [email protected]
Descargar

Click to add title - St. Louis .NET User Group