the microsoft technical roadshow 2007 language enhancements and linq daniel moth developer &...

48
The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd [email protected] http://www.danielmoth.com

Upload: jada-ashby

Post on 26-Mar-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

The Microsoft

Technical Roadshow 2007

Language Enhancements and LINQ

Daniel MothDeveloper & Platform GroupMicrosoft [email protected] http://www.danielmoth.com/Blog

Page 2: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

.NET Through The Ages

Page 3: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

AGENDA

C# v3 GoalsVB v9 GoalsIntroducing the goal: LINQRest of the talk

Language features that make LINQ work

Page 4: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

The Evolution of C#

C# 1.0

C# 2.0

C# 3.0

Components on a Managed Runtime

Generics

Language Integrated Query

Page 5: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

C# 3.0 Design Goals

Integrate objects, relational data, and XML

And

Increase conciseness of language

Add functional programming constructs

Don’t tie language to specific APIs

Remain 100% backwards compatible

Page 6: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Visual Basic 9.0

Simplify querying dataIntegrate query and transform operationsUnify query of object, relational, and XML data

Simplify working with XMLImpose structure on XML w/no schemaProduce XML documents quicklyAccess XML members easily

Page 7: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Simplify querying

Page 8: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

fromfrom c c inin Customers Customerswherewhere c.City == "Hove" c.City == "Hove"select select new { c.Name, c.Address };new { c.Name, c.Address };

FromFrom c c InIn Customers _ Customers _WhereWhere c.City = "Hove" _ c.City = "Hove" _Select Select c.Name, c.Addressc.Name, c.Address

Page 9: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

The Syntax

from id in source{ from id in source | join id in source on expr equals expr [ into id ] | let id = expr | where condition | orderby ordering, ordering, … } select expr | group expr by key[ into id query ]

Starts with fromStarts with from

Zero or more from, join, let, where, or orderby

Zero or more from, join, let, where, or orderby

Ends with select or group by

Ends with select or group by

Optional into continuationOptional into continuation

Page 10: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Language INtegrated Query (LINQ)

LINQ enabled data sourcesLINQ enabled data sources

LINQTo Objects

ObjectsObjects

LINQTo XML

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

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

XMXMLL

LINQ enabled ADO.NET

LINQTo DataSets

LINQTo SQL

LINQTo Entities

RelationRelationalal

Others…VB C#

.NET Language-Integrated Query

Page 11: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

The rest of this Talk

Language EnhancementsLocal Variable Type InferenceObject InitialisersAnonymous TypesLambda ExpressionsExtension Methods

+ Query Expressions = LINQ

Page 12: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Local Variable Type Inference

Page 13: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Local Variable Type Inference (c#)

int i = 666;string s = "Goodbye";double d = 3.14;int[] numbers = new int[] {1, 2, 3};Dictionary<int,Order> orders = new Dictionary<int,Order>();

var i = 666;var s = "Goodbye";var d = 3.14;var numbers = new int[] {1, 2, 3};var orders = new Dictionary<int,Order>();

“The type on the right hand side”“The type on the right hand side”

Page 14: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Type Inference (VB)

• Variable type inferred from initialiser

Dim x = 666Dim s = “Bye"Dim d = 3.14Dim numbers = New Integer() {1, 2, 3}Dim orders = new Dictionary(Of Integer, Order)()

IntegerIntegerStringString

DoubleDouble

Page 15: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Object Initialisers

Page 16: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Object Initialisers (c#)

public class Point{ private int x, y;

public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } }}

Point a = new Point { X = 0, Y = 1 };

Point a = new Point();a.X = 0;a.Y = 1;

Field or property assignments

Field or property assignments

Page 17: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Object Initialisers (VB)Public Class Point Private xx As Integer Public Property X() As Integer Get Return xx End Get Set(ByVal value As Integer) xx = value End Set End Property Public Y As Integer ‘or propertyEnd Class

Dim a As Point = New Point With { .X = 0, .Y = 1 }

Dim a As Point = New Point()a.X = 0a.Y = 1

Field or property assignments

Field or property assignments

Page 18: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Anonymous types

Page 19: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Anonymous Types (c#)

var o = new { Name = “Jenny”, Age = 31 };

class XXX{ public string Name; public int Age;}

class XXX{ public string Name; public int Age;}

XXXXXX

Page 20: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Anonymous Types (VB)

Dim o As Object = New With { .Name = “Jenny”, .Age = 31 }

Dim o = New With { .Name = “Jenny”, .Age = 31 }

XXXXXX

Class XXX Public Name As String Public Age As IntegerEnd Class

Class XXX Public Name As String Public Age As IntegerEnd Class

Page 21: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Lambda Expressions

Page 22: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Lambda Expressions

public delegate bool Predicate<T>(T obj);

public class List<T>{ public List<T> FindAll(Predicate<T> test) { List<T> result = new List<T>(); foreach (T item in this) if (test(item)) result.Add(item); return result; } …}

Page 23: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Lambda Expressions (c#)

public class MyClass{ public static void Main() { List<Customer> customers = GetCustomerList(); List<Customer> locals = customers.FindAll( new Predicate<Customer>(CityEqualsHove) ); }

static bool CityEqualsHove(Customer c) { return c.City == "Hove"; }}

Page 24: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Lambda Expressions (c#)

public class MyClass{ public static void Main() { List<Customer> customers = GetCustomerList(); List<Customer> locals = customers.FindAll( delegate(Customer c) { return c.City == "Hove"; } ); }}

Page 25: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Lambda Expressions (c#)

public class MyClass{ public static void Main() { List<Customer> customers = GetCustomerList(); List<Customer> locals = customers.FindAll( (Customer c) => {return c.City == "Hove";} ); }}

Lambda expression

Lambda expression

Page 26: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Lambda Expressions (c#)

public class MyClass{ public static void Main() { List<Customer> customers = GetCustomerList(); List<Customer> locals = customers.FindAll( c => c.City == "Hove" ); }}

Lambda expression

Lambda expression

Page 27: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Lambda Expressions (c#)

public class MyClass{ public static void Main() { List<Customer> customers = GetCustomerList(); List<Customer> locals = customers.FindAll(c => c.City == "Hove"); }}

Lambda expression

Lambda expression

Page 28: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Lambda Expressions (VB)

Public Function GetTheLocals() As List(Of Customer) Dim customers As List(Of Customer) = GetCustomerList()

return customers.FindAll(AddressOf CityEqualsHove)End Function

Function CityEqualsHove(ByVal c As Customer) As Boolean Return c.City = "Hove"End Function

Public Function GetTheLocals() As List(Of Customer) Dim customers As List(Of Customer) = GetCustomerList()

return customers.FindAll(Function(c) c.City = "Hove")End Function

Lambda expression

Lambda expression

Page 29: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Extension Methods

Page 30: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Extension Methods (c#)

namespace MyStuff{ public static class Extensions { public static string Concatenate(this IEnumerable<string> strings, string separator) {…} }}

using MyStuff;

string[] names = new string[] { "Jenny", "Daniel", "Rita" };string s = names.Concatenate(", ");

Extension methodExtension method

Brings extensions into scopeBrings extensions into scope

obj.Foo(x, y)

XXX.Foo(obj, x, y)

obj.Foo(x, y)

XXX.Foo(obj, x, y)

IntelliSense!IntelliSense!

Page 31: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Namespace ArrayExtensions <Extension()> _ Module IntArrExtensions

<Extension()> _ Function Sort(i As Integer()) As Integer() … End Function

<Extension()> _ Sub ReverseInPlace(ByRef i As Integer()) … End Property End ExtensionEnd Namespace

Extension Methods (VB)

• Extend existing types with new methods

Imports ArrayExtensions

Dim values() As Integer = {5, 4, 2, 1, 3}Console.WriteLine(IntegerArrExtensions.ReverseInPlace( _

IntegerArrExtensions.Sort(values))

Dim values() As Integer = GetValues()Console.WriteLine(values.Sort().ReverseInPlace())

obj.Foo(x, y)

XXX.Foo(obj, x, y)

obj.Foo(x, y)

XXX.Foo(obj, x, y)

Page 32: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Bringing it all together andintroducing the System.Linq namespace

Page 33: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

from c in customerswhere c.City == "Hove"select new { c.Name, c.Phone };

customers.Where(c => c.City == "Hove").Select(c => new { c.Name, c.Phone });

Query Expressions (c#)

Queries translate to method invocationsWhere, Join, OrderBy, Select, GroupBy, …

Page 34: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Query Expressions (VB)Dim highThreadProcs = _ From proc In Process.GetProcesses _ Where proc.Threads.Count > 10 _ Select proc.ProcessName, proc.Threads.Count

Dim highThreadProcs = Process.GetProcesses(). _ Where(Function(proc As Process) proc.Threads.Count > 10). _ Select (Function(proc As Process) _

New With {.ProcessName = proc.ProcessName _

.Count = proc.Threads.Count)Function _Filter1(proc As Process) As Boolean Return proc.Threads.Count > 10End Function

Function _Projection1(proc As Process) As <Anonymous Type> Dim projection As New <AnonymousType> projection.ProcessName = proc.ProcessName projection.Count = proc.Threads.Count Return projection End Function

Page 35: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Query Expressions

from id in source{ from id in source | join id in source on expr equals expr [ into id ] | let id = expr | where condition | orderby ordering, ordering, … } select expr | group expr by key[ into id query ]

Starts with fromStarts with from

Zero or more from, join, let, where, or orderby

Zero or more from, join, let, where, or orderby

Ends with select or group by

Ends with select or group by

Optional into continuationOptional into continuation

Page 36: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Query ExpressionsQuery Expressions

ProjectProject SelectSelect <expr><expr>

FilterFilter WhereWhere <<exprexpr>, >, DistinctDistinct

TestTest Any(Any(<<exprexpr>>)), , All(All(<<exprexpr>>))

JoinJoin <<exprexpr> > JoinJoin < <exprexpr> > OnOn < <exprexpr> > EqualsEquals < <exprexpr>>

GroupGroup GroupGroup ByBy <<exprexpr>, <>, <exprexpr> > Into Into <expr<expr>, <>, <exprexpr>>

GroupGroup Join Join <<decldecl> > OnOn < <exprexpr> > EqualsEquals < <exprexpr> > IntoInto < <exprexpr>>

AggregatAggregatee

Count(Count(<<exprexpr>>)), , Sum(Sum(<<exprexpr>>)), , Min(Min(<<exprexpr>>)), , Max(Max(<expr><expr>)), , Avg(Avg(<<exprexpr>>))

PartitionPartition Skip Skip [ [ WhileWhile ] ] <<exprexpr>, >, Take Take [ [ WhileWhile ] ] <<exprexpr>>

SetSet UnionUnion, , IntersectIntersect, , ExceptExcept

OrderOrder OrderOrder ByBy <<exprexpr>, <>, <exprexpr> > [ [ Ascending | Ascending | DescendingDescending ] ]

Page 37: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

More Examples of LINQ queries

Page 38: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

C# 3.0 Language Innovations

var contacts = from c in customers where c.City == "Hove" select new { c.Name, c.Phone };

var contacts = customers .Where(c => c.City == "Hove") .Select(c => new { c.Name, c.Phone });

Extension methodsExtension methods

Lambda expressions

Lambda expressions

Query expressions

Query expressions

Object initializers

Object initializers

Anonymous types

Anonymous types

Local variable type inferenceLocal variable type inference

Page 39: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

VB 9.0 Language Innovations

Dim contacts = From c In customers _ Where c.City = "Hove" _ Select c.Name, c.Phone

Dim contacts = _ customers _ .Where(Function(c) c.City = "Hove") _ .Select(Function(c) New With { c.Name, c.Phone })

Extension methodsExtension methods

Lambda expressions

Lambda expressions

Query expressions

Query expressions

Object initializers

Object initializersAnonymous

typesAnonymous

types

Local variable type inferenceLocal variable type inference

Page 40: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

BEFORE THE SUMMARY,ONE LAST FEATURE...

Page 41: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Expression Trees (VB)

Dim f As Predicate(Of Customer) = Function (c) c.City = "Hove"

Dim e As Expression(Of Predicate(Of Customer)) = Function (c) c.City = "Hove"

Dim e As Expression(Of Predicate(Of Customer)) = _ New Expression(Of Predicate(Of Customer))( New BinaryExpression(ExpressionType.EQ, New PropertyExpression( New ParameterExpression(0), GetType(Customer).GetProperty("City") ), New ConstantExpression("Hove") ) )

System.Linq.Expressions.Expression(Of T)where T is a delegate type

System.Linq.Expressions.Expression(Of T)where T is a delegate type

Page 42: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Expression Trees (c#)

Expression<Predicate<Customer>> test = c => c.City == "Hove";

public delegate bool Predicate<T>(T item);

ParameterExpression c = Expression.Parameter(typeof(Customer), "c");Expression expr = Expression.Equal( Expression.Property(c, typeof(Customer).GetProperty("City")), Expression.Constant("Hove") );Expression<Predicate<Customer>> test = Expression.Lambda<Predicate<Customer>>(expr, c);

Page 43: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Expression Trees (c#)

Predicate<Customer> test = c => c.City == "Hove";

Predicate<Customer> test = new Predicate<Customer>(XXX);

private static bool XXX(Customer c) { return c.City == "Hove";}

public delegate bool Predicate<T>(T item);

Page 44: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

LINQ Architecture

System.Linq.EnumerableDelegate based

System.Linq.EnumerableDelegate based

Source implementsSource implementsIEnumerable<T>IEnumerable<T>

var query = from c in customers where c.City == "Hove" select c.Name;

var query = customers.Where(c => c.City == "Hove").Select(c => c.Name);

System.Linq.QueryableExpression tree based

System.Linq.QueryableExpression tree based

Source implementsSource implementsIQueryable<T>IQueryable<T>

SQLSQLSQLSQL DataSetsDataSetsDataSetsDataSetsObjectsObjectsObjectsObjects Others…Others…Others…Others…

Page 45: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Later today....

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

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

Page 46: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

Summary

LINQ over various sourcesObjects, SQL, XML, other

LINQ builds on other new featuresType inference, object initialisers, anonymous types, extension methods, lambda expressionsEnumerable and Queryable from System.LinqQuery Expressions

Lamdas bound to expression trees

Page 47: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

http://www.danielmoth.com/Blog

Page 48: The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd daniel.moth@microsoft.com

© 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.