18
The .NET Language The .NET Language Integrated Query Integrated Query Project Project Anders Hejlsberg Anders Hejlsberg TLN306 TLN306 Technical Fellow Technical Fellow Microsoft Corporation Microsoft Corporation

The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

Embed Size (px)

Citation preview

Page 1: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

The .NET LanguageThe .NET LanguageIntegrated Query ProjectIntegrated Query Project

Anders HejlsbergAnders HejlsbergTLN306TLN306Technical FellowTechnical FellowMicrosoft CorporationMicrosoft Corporation

Page 2: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

Problem:Problem:Data != ObjectsData != Objects

Page 3: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

The LINQ ProjectThe LINQ Project

StandardStandardQueryQuery

OperatorsOperators

ObjectsObjects

DLinqDLinq(ADO.NET)(ADO.NET)

XLinqXLinq(System.Xml)(System.Xml)

<book> <title/> <author/> <year/> <price/></book>

XMLXML

.NET Language Integrated Query.NET Language Integrated Query

C#C# VBVB Others…Others…

SQLSQL WinFWinFSS

Page 4: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

The LINQ ProjectThe LINQ Project

Page 5: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

Language InnovationsLanguage Innovations

Lambda expressionsLambda expressions

Extension methodsExtension methods

Local variable type inferenceLocal variable type inference

Object initializersObject initializers

Anonymous typesAnonymous types

Query expressionsQuery expressions

Expression treesExpression trees

var x = 5;var x = 5;

static void Dump(this static void Dump(this object o);object o);

c => c => c.Namec.Name

new Point { x = 1, y new Point { x = 1, y = 2 }= 2 }

new { c.Name, new { c.Name, c.Phone }c.Phone }

from … where … from … where … selectselect

Expression<TExpression<T>>

Page 6: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

Standard Query OperatorsStandard Query OperatorsRestrictionRestriction WhereWhere

ProjectionProjection Select, SelectManySelect, SelectMany

OrderingOrdering OrderBy, ThenByOrderBy, ThenBy

GroupingGrouping GroupByGroupBy

QuantifiersQuantifiers Any, AllAny, All

PartitioningPartitioning Take, Skip, TakeWhile, SkipWhileTake, Skip, TakeWhile, SkipWhile

SetsSets Distinct, Union, Intersect, ExceptDistinct, Union, Intersect, Except

ElementsElements First, FirstOrDefault, ElementAtFirst, FirstOrDefault, ElementAt

AggregationAggregation Count, Sum, Min, Max, AverageCount, Sum, Min, Max, Average

ConversionConversion ToArray, ToList, ToDictionaryToArray, ToList, ToDictionary

CastingCasting OfType<T>OfType<T>

Page 7: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

Deferred Query ExecutionDeferred Query ExecutionCustomer[] custs = SampleData.GetCustomers();Customer[] custs = SampleData.GetCustomers();

custscusts

PhonePhoneNameNameIDID

var query = from c in custs where c.City == "London" select var query = from c in custs where c.City == "London" select c.Name;c.Name;

var query = custs.Where(c => c.City == "London").Select(c var query = custs.Where(c => c.City == "London").Select(c => c.Name);=> c.Name);

SelectSelect

c => c => c.Namec.Name

string[] names = query.ToArray();string[] names = query.ToArray();

namesnames

c => c.City == c => c.City == "London""London"

WhereWhere

Page 8: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

DLinq For Relational DataDLinq For Relational Data

SqlConnection c = new SqlConnection(…);SqlConnection c = new SqlConnection(…);c.Open();c.Open();SqlCommand cmd = new SqlCommand(SqlCommand cmd = new SqlCommand( @"SELECT c.Name, c.Phone@"SELECT c.Name, c.Phone FROM Customers cFROM Customers c WHERE c.City = @p0");WHERE c.City = @p0");cmd.Parameters.AddWithValue("@p0", cmd.Parameters.AddWithValue("@p0", "London“);"London“);DataReader dr = c.Execute(cmd);DataReader dr = c.Execute(cmd);while (dr.Read()) {while (dr.Read()) { string name = dr.GetString(0);string name = dr.GetString(0); string phone = dr.GetString(1);string phone = dr.GetString(1); DateTime date = dr.GetDateTime(2);DateTime date = dr.GetDateTime(2);}}dr.Close();dr.Close();

Accessing data todayAccessing data todayQueries in Queries in

quotesquotes

Loosely Loosely bound bound

argumentsarguments

Loosely Loosely typed result typed result

setssets

No compile No compile time checkstime checks

Page 9: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

public class Customer { … }public class Customer { … }

public class Northwind: DataContextpublic class Northwind: DataContext{{ public Table<Customer> Customers;public Table<Customer> Customers; … …}}

Northwind db = new Northwind(…);Northwind db = new Northwind(…);var contacts =var contacts = from c in db.Customersfrom 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 DataDLinq For Relational Data

Accessing data with DLinqAccessing data with DLinqClasses Classes

describe datadescribe data

Strongly Strongly typed typed

connectionconnection

Integrated Integrated query syntaxquery syntax

Strongly Strongly typed resultstyped results

Tables are Tables are like like

collectionscollections

Page 10: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

DLinq For Relational DataDLinq For Relational Data

Language integrated data accessLanguage integrated data accessMaps tables and rows to classes and Maps tables and rows to classes and objectsobjects

Builds on ADO.NET and .NET Builds on ADO.NET and .NET TransactionsTransactions

MappingMappingEncoded in attributesEncoded in attributes

Relationships map to propertiesRelationships map to properties

PersistencePersistenceAutomatic change trackingAutomatic change tracking

Updates through SQL or stored Updates through SQL or stored proceduresprocedures

Page 11: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

XLinq For XML DataXLinq For XML Data

XmlDocument doc = new XmlDocument();XmlDocument doc = new XmlDocument();XmlElement contacts = doc.CreateElement("contacts");XmlElement contacts = doc.CreateElement("contacts");foreach (Customer c in customers)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);doc.AppendChild(contacts);

Programming XML todayProgramming XML today

<contacts><contacts> <contact><contact> <name>Great Lakes <name>Great Lakes Food</name>Food</name> <phone>(503) <phone>(503) 555-7123</phone>555-7123</phone> </contact></contact> … …</contacts></contacts>

Imperative Imperative modelmodel

Document Document centriccentric

No integrated No integrated queriesqueries

Memory Memory intensiveintensive

Page 12: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

XLinq For XML DataXLinq For XML Data

XElement contacts = new XElement("contacts",XElement contacts = new XElement("contacts", from c in customersfrom 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 XLinqProgramming XML with XLinqDeclarative Declarative

modelmodel

ElementElementcentriccentric

Integrated Integrated queriesqueries

Smaller and Smaller and fasterfaster

Page 13: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

XLinq For XML DataXLinq For XML Data

Language integrated query for XMLLanguage integrated query for XMLExpressive power of XPath / XQueryExpressive power of XPath / XQuery

But with C# or VB as programming But with C# or VB as programming languagelanguage

Leverages experience with DOMLeverages experience with DOMElement centric, not document centricElement centric, not document centric

Functional constructionFunctional construction

Text nodes are just stringsText nodes are just strings

Simplified XML namespace supportSimplified XML namespace support

Faster and smallerFaster and smaller

Page 14: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

DLinq and XLinqDLinq and XLinq

Page 15: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

The LINQ ProjectThe LINQ Project

Language Integrated Query for .NETLanguage Integrated Query for .NETNative query syntax in C# 3.0 and VB Native query syntax in C# 3.0 and VB 9.09.0

Standard Query OperatorsStandard Query OperatorsSQL-like queries for any .NET collectionSQL-like queries for any .NET collection

DLinqDLinqQuery enabled data access frameworkQuery enabled data access framework

XLinqXLinqQuery enabled, smaller, faster XML DOMQuery enabled, smaller, faster XML DOM

Page 16: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

Benefits Of LINQBenefits Of LINQ

Unified querying of objects, Unified querying of objects,

relational, XMLrelational, XML

Type checking and IntelliSense for Type checking and IntelliSense for

queries queries

SQL and XQuery-like power in C# SQL and XQuery-like power in C#

and VBand VB

Extensibility model for languages / Extensibility model for languages /

APIsAPIs

Page 17: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

More InformationMore InformationWednesdayWednesday ThursdayThursday FridayFriday

TLN306TLN306 - LINQ Overview - LINQ Overview1:45 PM - 3:00 PM 1:45 PM - 3:00 PM

Halls C & DHalls C & D

TLN308TLN308 - VB 9.0  - VB 9.0 10:00 AM - 11:15 AM 10:00 AM - 11:15 AM

Room 411Room 411

DAT323 (R)DAT323 (R) - DLinq for  - DLinq for SQLSQL

8:30 AM - 9:45 AM 8:30 AM - 9:45 AM Room 408 ABRoom 408 AB

TLN307TLN307 - C# 3.0 - C# 3.03:15 PM - 4:30 PM 3:15 PM - 4:30 PM

Halls C & DHalls C & D

TLN307 (R)TLN307 (R) - C# 3.0 - C# 3.02:15 PM - 3:30 PM 2:15 PM - 3:30 PM

Room 402 ABRoom 402 AB

DAT324 (R)DAT324 (R) - XLinq - XLinq10:30 AM -11:45 AM 10:30 AM -11:45 AM

Room 408 ABRoom 408 AB

DAT312DAT312 - DLinq for  - DLinq for WinFSWinFS

5:00 PM – 6:15 PM 5:00 PM – 6:15 PM Room 515 ABRoom 515 AB

DAT323DAT323 - DLinq for SQL - DLinq for SQL2:15 PM - 3:30 PM 2:15 PM - 3:30 PM

Room 152/153 (Hall F)Room 152/153 (Hall F)

PNL11PNL11 - LINQ Panel - LINQ Panel1:00 PM - 2:30 PM 1:00 PM - 2:30 PM

Room 152/153 (Hall F)Room 152/153 (Hall F)

DAT324DAT324 - XLinq - XLinq5:15 PM – 6:30 PM 5:15 PM – 6:30 PM

Room 404Room 404

Ask the ExpertsAsk the Experts6:30 PM – 9:00 PM 6:30 PM – 9:00 PM

Main HallMain Hallhttp://msdn.microsoft.com/netframework/future/linq/http://msdn.microsoft.com/netframework/future/linq/

Page 18: The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation

© 2005 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.