The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd

Slides:



Advertisements
Similar presentations
Language Integrated Query (LINQ) Martin Parry Developer & Platform Group Microsoft Ltd
Advertisements

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Preface Demo A Quick Thank You How Did We Do It?
Feature: Identity Management - Login © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training
© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
.NET 3.5 – Mysteries. NetFx Evolution NetFx 1.0 C# 1.0, VB 7.0, VS.NET NetFx 1.1 C# 1.1, VB 7.1, VS 2003 NetFx 2.0 C# 2.0, VB 8.0, VS 2005 NetFx 3.0 C#
Feature: Reprint Outstanding Transactions Report © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
Feature: Financial Enhancements © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may.
Feature: Purchase Requisitions - Requester © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
MIX 09 4/15/ :14 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Feature: Payroll and HR Enhancements © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
Visual Studio 2008 and ASP.NET 3.5 Mike Ormond Developer & Platform Group Microsoft Ltd
The Microsoft Technical Roadshow 2007 Advances for Data in VS “Orcas” Mike Taulty Developer & Platform Group Microsoft Ltd
What’s new in ASP.NET 3.5? Mike Ormond Developer & Platform Group Microsoft Ltd
Windows 7 Training Microsoft Confidential. Windows ® 7 Compatibility Version Checking.
Feature: Purchase Order Prepayments II © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: OLE Notes Migration Utility
Feature: Web Client Keyboard Shortcuts © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
Feature: SmartList Usability Enhancements © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
Session 1.
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
Built by Developers for Developers…. © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
 Rico Mariani Architect Microsoft Corporation.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Assign an Item to Multiple Sites © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Print Remaining Documents © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
Introduction to LINQ Lecture # 19 August Introduction How do you interrogate/manipulate data? What if you could do the work in a type-safe," string-free.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Document Attachment –Replace OLE Notes © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
Feature: Suggested Item Enhancements – Sales Script and Additional Information © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows.
Feature: Customer Combiner and Modifier © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
Feature: Employee Self Service Timecard Entry © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
SQL Server SQL Azure Visual Studio“Quadrant” SQL Server Modeling Services Entity Framework ADO.NET“M”/EDM Data Services …
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.

customer.
C#: Future Directions in Language Innovation Anders Hejlsberg TLN307 Technical Fellow Microsoft Corporation.
demo © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
demo Demo.
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
Feature: Void Historical/Open Transaction Updates © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
demo QueryForeign KeyInstance /sm:body()/x:Order/x:Delivery/y:TrackingId1Z
Feature: Suggested Item Enhancements – Analysis and Assignment © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and.
Windows Azure SQL Data Sync Name Title Microsoft Corporation.
projekt202 © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
The CLR CoreCLRCoreCLR © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
Joel Pobar Language Geek Microsoft DEV320 Improve on C# % Backwards Compatible Language Integrated Query (LINQ)
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks.
Jonathan Aneja Program Manager Microsoft Corporation Session Code: DTL336 Anders Hejlsberg Technical Fellow Microsoft Corporation.
 Boris Jabes Program Manager Lead Microsoft Corporation TL13.
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
Sr. Dir. – Systems Architecture Inlet Technologies.
Language Integrated Query Mike Taulty Developer & Platform Group Microsoft Ltd

Mark Michaelis Chief Computer Nerd IDesign/Itron/IntelliTechture DTL313.
Tech·Ed North America /18/2018 2:05 PM
Upgrading Your C# Programming Skills to Be a More Effective Developer
Title of Presentation 12/2/2018 3:48 PM
8/04/2019 9:13 PM © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Виктор Хаджийски Катедра “Металургия на желязото и металолеене”
Технологии доступа к данным на платформе Microsoft.NET
Шитманов Дархан Қаражанұлы Тарих пәнінің
Title of Presentation 5/24/2019 1:26 PM
Presentation transcript:

The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd

.NET Through The Ages

AGENDA C# v3 Goals VB v9 Goals Introducing the goal: LINQ Rest of the talk Language features that make LINQ work

The Evolution of C# C# 1.0 C# 2.0 C# 3.0 Components on a Managed Runtime Generics Language Integrated Query

C# 3.0 Design Goals Integrate objects, relational data, and XML And Increase conciseness of language Add functional programming constructs Dont tie language to specific APIs Remain 100% backwards compatible

Visual Basic 9.0 Simplify querying data Integrate query and transform operations Unify query of object, relational, and XML data Simplify working with XML Impose structure on XML w/no schema Produce XML documents quickly Access XML members easily

Simplify querying

from c in Customers where c.City == "Hove" select new { c.Name, c.Address }; From c In Customers _ Where c.City = "Hove" _ Select c.Name, c.Address

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 from Zero or more from, join, let, where, or orderby Ends with select or group by Ends with select or group by Optional into continuation

Language INtegrated Query (LINQ) LINQ enabled data sources LINQ To Objects Objects LINQ To XML XML LINQ enabled ADO.NET LINQ To DataSets LINQ To SQL LINQ To Entities Relational Others… VB C#.NET Language-Integrated Query

The rest of this Talk Language Enhancements Local Variable Type Inference Object Initialisers Anonymous Types Lambda Expressions Extension Methods + Query Expressions = LINQ

Local Variable Type Inference

Local Variable Type Inference (c#) int i = 666; string s = "Goodbye"; double d = 3.14; int[] numbers = new int[] {1, 2, 3}; Dictionary orders = new Dictionary (); int i = 666; string s = "Goodbye"; double d = 3.14; int[] numbers = new int[] {1, 2, 3}; Dictionary orders = new Dictionary (); var i = 666; var s = "Goodbye"; var d = 3.14; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary (); var i = 666; var s = "Goodbye"; var d = 3.14; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary (); The type on the right hand side

Type Inference (VB) Variable type inferred from initialiser Dim x = 666 Dim s = Bye" Dim d = 3.14 Dim numbers = New Integer() {1, 2, 3} Dim orders = new Dictionary(Of Integer, Order)() Dim x = 666 Dim s = Bye" Dim d = 3.14 Dim numbers = New Integer() {1, 2, 3} Dim orders = new Dictionary(Of Integer, Order)() Integer String Double

Object Initialisers

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; } } } 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; Point a = new Point(); a.X = 0; a.Y = 1; Field or property assignments

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 property End Class 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 property End Class Dim a As Point = New Point With {.X = 0,.Y = 1 } Dim a As Point = New Point() a.X = 0 a.Y = 1 Dim a As Point = New Point() a.X = 0 a.Y = 1 Field or property assignments

Anonymous types

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; } XXX

Anonymous Types (VB) Dim o As Object = New With {.Name = Jenny,.Age = 31 } Dim o = New With {.Name = Jenny,.Age = 31 } XXX Class XXX Public Name As String Public Age As Integer End Class Class XXX Public Name As String Public Age As Integer End Class

Lambda Expressions

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

Lambda Expressions (c#) public class MyClass { public static void Main() { List customers = GetCustomerList(); List locals = customers.FindAll( new Predicate (CityEqualsHove) ); } static bool CityEqualsHove(Customer c) { return c.City == "Hove"; } public class MyClass { public static void Main() { List customers = GetCustomerList(); List locals = customers.FindAll( new Predicate (CityEqualsHove) ); } static bool CityEqualsHove(Customer c) { return c.City == "Hove"; }

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

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

Lambda Expressions (c#) public class MyClass { public static void Main() { List customers = GetCustomerList(); List locals = customers.FindAll( c => c.City == "Hove" ); } public class MyClass { public static void Main() { List customers = GetCustomerList(); List locals = customers.FindAll( c => c.City == "Hove" ); } Lambda expression

Lambda Expressions (c#) public class MyClass { public static void Main() { List customers = GetCustomerList(); List locals = customers.FindAll(c => c.City == "Hove"); } public class MyClass { public static void Main() { List customers = GetCustomerList(); List locals = customers.FindAll(c => c.City == "Hove"); } Lambda expression

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(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 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

Extension Methods

Extension Methods (c#) namespace MyStuff { public static class Extensions { public static string Concatenate(this IEnumerable strings, string separator) {…} } namespace MyStuff { public static class Extensions { public static string Concatenate(this IEnumerable strings, string separator) {…} } using MyStuff; string[] names = new string[] { "Jenny", "Daniel", "Rita" }; string s = names.Concatenate(", "); string[] names = new string[] { "Jenny", "Daniel", "Rita" }; string s = names.Concatenate(", "); Extension method Brings extensions into scope obj.Foo(x, y) XXX.Foo(obj, x, y) obj.Foo(x, y) XXX.Foo(obj, x, y) IntelliSense!

Namespace ArrayExtensions _ Module IntArrExtensions _ Function Sort(i As Integer()) As Integer() … End Function _ Sub ReverseInPlace(ByRef i As Integer()) … End Property End Extension End Namespace Namespace ArrayExtensions _ Module IntArrExtensions _ Function Sort(i As Integer()) As Integer() … End Function _ Sub ReverseInPlace(ByRef i As Integer()) … End Property End Extension End 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 = {5, 4, 2, 1, 3} Console.WriteLine(IntegerArrExtensions.ReverseInPlace( _ IntegerArrExtensions.Sort(values)) Dim values() As Integer = GetValues() Console.WriteLine(values.Sort().ReverseInPlace()) 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)

Bringing it all together and introducing the System.Linq namespace

from c in customers where 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 invocations Where, Join, OrderBy, Select, GroupBy, …

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 > 10 End Function Function _Projection1(proc As Process) As Dim projection As New projection.ProcessName = proc.ProcessName projection.Count = proc.Threads.Count Return projection End Function

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 from Zero or more from, join, let, where, or orderby Ends with select or group by Ends with select or group by Optional into continuation

Query Expressions Project Select Select Filter Where, Distinct Test Any( ), All( ) Join Join On Equals Join On Equals Group Group By, Into, Group By, Into, Group Join On Equals Into Group Join On Equals Into Aggregate Count( ), Sum( ), Min( ), Max( ), Avg( ) Partition Skip [ While ], Take [ While ] Skip [ While ], Take [ While ] Set Union, Intersect, Except Order Order By, [ Ascending | Descending ]

More Examples of LINQ queries

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 methods Lambda expressions Query expressions Object initializers Anonymous types Local variable type inference

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 methods Lambda expressions Query expressions Object initializers Anonymous types Local variable type inference

BEFORE THE SUMMARY, ONE LAST FEATURE...

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") ) 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

Expression Trees (c#) Expression > test = c => c.City == "Hove"; public delegate bool Predicate (T item); ParameterExpression c = Expression.Parameter(typeof(Customer), "c"); Expression expr = Expression.Equal( Expression.Property(c, typeof(Customer).GetProperty("City")), Expression.Constant("Hove") ); Expression > test = Expression.Lambda >(expr, c); ParameterExpression c = Expression.Parameter(typeof(Customer), "c"); Expression expr = Expression.Equal( Expression.Property(c, typeof(Customer).GetProperty("City")), Expression.Constant("Hove") ); Expression > test = Expression.Lambda >(expr, c);

Expression Trees (c#) Predicate test = c => c.City == "Hove"; Predicate test = new Predicate (XXX); private static bool XXX(Customer c) { return c.City == "Hove"; } private static bool XXX(Customer c) { return c.City == "Hove"; } public delegate bool Predicate (T item);

LINQ Architecture System.Linq.Enumerable Delegate based Source implements IEnumerable Source implements IEnumerable 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.Queryable Expression tree based Source implements IQueryable Source implements IQueryable SQLSQLDataSetsDataSetsObjectsObjectsOthers…Others…

Later today....

Summary LINQ over various sources Objects, SQL, XML, other LINQ builds on other new features Type inference, object initialisers, anonymous types, extension methods, lambda expressions Enumerable and Queryable from System.Linq Query Expressions Lamdas bound to expression trees

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