Presentation is loading. Please wait.

Presentation is loading. Please wait.

Paul Yuknewicz Lead Program Manager Microsoft Visual Basic DEV 319.

Similar presentations


Presentation on theme: "Paul Yuknewicz Lead Program Manager Microsoft Visual Basic DEV 319."— Presentation transcript:

1

2 Paul Yuknewicz paulyuk@microsoft.com Lead Program Manager Microsoft Visual Basic DEV 319

3 “For example, I personally believe that Visual Basic did more for programming than “Object-Oriented Languages” did. Yet people laugh at Visual Basic and say it’s a bad language, and they’ve been talking about OO languages for decades. And no, Visual Basic wasn’t a great language, but I think the easy DB interfaces in Visual Basic were fundamentally more important than object orientation is, for example.”

4 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

5 Language-INtegrated Query (LINQ) Language Features (through LINQ to Objects) LINQ to Relational Data LINQ to DataSet LINQ to SQL LINQ to Entities LINQ to XML and XML Integration … and of course lots of demos!

6 LINQ enabled data sources LINQ To ObjectsTo Objects Objects LINQ To XMLTo XML XML LINQ-enabled ADO.NETLINQ To DatasetsTo DatasetsLINQ To SQL LINQ To EntitiesTo Entities Relational Others… Visual BasicVisual Basic C#.NET Language-Integrated Query.NET Language-Integrated Query

7

8 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

9 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 ]

10 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 ]

11 Dim c As New SqlConnection(…) c.Open() Dim cmd As SqlCommand( _ "SELECT c.Name, c.Phone “ & _ "FROM Customers c” & _ "WHERE c.City = @p0") cmd.Parameters("@p0“) = "London" Dim dr As DataReader= c.Execute(cmd) While (dr.Read()) Dim name As String = r.GetString(0) Dim phone As String= r.GetString(1) Dim date As DateTime = r.GetDateTime(2) End While r.Close() Accessing data today Queries in quotes Loosely bound arguments Loosely typed result sets No compile time checks

12 Public Class Customer … Public Class Northwind Inherits DataContext Public Property Customers As Table(Of Customer) … End Class Dim db As New Northwind(…) Dim contacts = _ From cust in db.Customers _ Where cust.City = "London" Select cust.Name, cust.Phone For Each custInfo in contacts ColdCall(custInfo.Name, custInfo.Phone) Next Accessing data with LINQ Classes describe data Strongly typed connection Integrated query syntax Strongly typed results Tables are like collections

13

14 From before Select Fully composable Dim customers = _ From cust In db.Customers _ Select cust.Name, cust.City, cust.State, cust.ZIP Dim customers = _ From cust In db.Customers _ Select cust.Name, cust.City, cust.State, cust.ZIP _ Order By ZIP _ Select Name, City, State Operations are “built up” line-by-line and… Enables good IntelliSense Dim aCustomers = _ From cust In customers _ Where cust.Name.StartsWith(“A”) _ Select cust.Name, cust.City …across statements…across statements

15 Aggregation is explicit Dim customers = _ From o In Orders _ Group By o.CustomerID _ Into OrderTotal = Sum(o.Amount * o.Price) _ Select CustomerID, OrderTotal Grouping keyGrouping key Explicit aggregation Includes operators for hierarchical data Dim procs= _ From proc In System.Diagnostics.Process.GetProcesses( ) _ Aggregate thread In proc.Threads _ Into AvgThreadPriority = Average(thread.CurrentPriority) _ Select Name = proc.ProcessName, AvgThreadPriority Group already exists

16 Dim doc As New XmlDocument() Dim contacts As XMLElement = doc.CreateElement("contacts") For Each Dim c in Customers If (c.Country = "USA") Dim e As XMLElement = doc.CreateElement("contact") Dim name As XMLElement = doc.CreateElement("name") name.InnerText = c.CompanyName e.AppendChild(name) Dim phone As XMLElement = doc.CreateElement("phone") phone.InnerText = c.Phone e.AppendChild(phone) contacts.AppendChild(e) End If Next doc.AppendChild(contacts) <contacts> Great Lakes Food Great Lakes Food (503) 555-7123 (503) 555-7123 …</contacts> Imperative model Document- centric No integrated queries Memory- intensive

17 Dim contacts As New XElement("contacts", _ From cust in customers _ Where cust.Country = "USA“ _ Select New XElement("contact", _ New XElement("name", cust.CompanyName), _ New XElement("phone", cust.Phone) _ ) Declarative model Element-centric Integrated queries Smaller and faster

18 Dim contacts = _ <%= _ From cust In customers _ Where cust.Country = "USA" _ Select _ %> Infers Xml.Linq XElement No conceptual barrier Expression holes for computed values

19

20 Shorthand for object creation Dim emp = _ Joe 28 Engineering Dim emp = _ New XElement("employee", _ New XElement("name", "Joe"), _ New XElement("age", 28), _ New XElement("department", _ New XElement("name", "Engineering"), _ New XAttribute("id", 432)))

21 Element access covers all XML axes Dim employees As XElement = GetCurrentEmployeesByDept(“IT”) Dim deptID As Integer = CInt(employees.Attribute(“DeptID")) Dim emp As XElement = First(employees.Descendents(“Employee")) Dim empDOB As Date = CDate(item.Element(“DateOfBirth“).Value) Dim employees As XElement = GetCurrentEmployeesByDept(“IT”) Dim deptID As Integer = CInt(employees.@DeptID) Dim emp As XElement = First(employees… ) Dim empDOB As Date = CDate(emp..Value) Attributes Descendents Elements

22 Language integrated query for XML Expressive power of XPath/XQuery But with Visual Basic as programming language No conceptual barrier between XML and code Leverages experience with DOM Element-centric, not document-centric Symmetry in element/attribute APIs Functional construction Text nodes are just strings Simplified XML namespace support Faster and smaller than DOM

23 Query expressions XML literals XML element access Nullable types Object initializers Local type inference Lambda expressions Extension methods Expression trees Anonymous types Ternary operator Coalesce operator Relaxed delegates Partial methods …and many other bug fixes and small features!

24 DEV318 - Strategies for Moving Your Microsoft Visual Basic 6 Investments to.NET 14/08/2007 10:45 AM - 12:00 PM DEV319 - LINQ and XML for the Microsoft Visual Basic Developer 14/08/2007 2:20 PM - 3:35 PM DEV317 - Microsoft Visual Basic: Tips and Tricks for the Microsoft Visual Studio 2008 IDE 15/08/2007 9:00 AM - 10:15 AM

25 Required slide VB Team Blog http://blogs.msdn.com/vbteam Visual C# Developer Center http://msdn2.microsoft.com/en-us/vcsharp/default.aspx Visual Studio Orcas Beta2 Download Page http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx Visual Basic Developer Center & Content http://msdn.com/vbasic

26

27

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

29 LINQ to SQL Emphasis on rapid application development Direct mapping to Microsoft SQL Server family of databases Available in Orcas LINQ to Entities Focus on enterprise-grade data scenarios Flexible mapping to relational data Access to Microsoft SQL Server family and other databases through ADO.NET provider model Available in first half of 2008 CTP’s and Betas on top of Orcas releases through 2007

30 Implicitly-typed locals Extension methods Lambda expressions Object initializers Anonymous types Nullable types Query expressions Dim x = 5 <Extension> Sub Randomize(col As Collection) Sub Randomize(col As Collection) Function(c) c.Name New Point With {.x = 1,.y = 2 } New With { c.Name, c.Phone } From … Where … Select If emp.DOB >= Today

31 Variable type inferred from initializer Dim x = 5 Dim s = "Hello" Dim d = 1.0 Dim numbers = New Integer() {1, 2, 3} Dim orders = new Dictionary(Of Integer, Order)() All types are Object! Integer String Double

32 Namespace ArrayExtensions Module IntArrExtensions _ Function Sort(i As Integer()) As Integer() … End Function _ Sub ReverseInPlace(ByRef i As Integer()) … End Sub End Module End Namespace 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)

33 Return _ Process.GetProcesses(). _ Where( Function(proc As Process) proc.Threads.Count > 10). _ Select( Function(proc As Process) _ New With {.Name = proc.ProcessName, _.ThreadCount = proc.Threads.Count } ) Function _Filter1(proc As Process) As Boolean Return proc.Threads.Count >10 End Function Function _Project1(proc As Process) As Return New With {.Name = proc.ProcessName, _.ThreadCount = proc.Threads.Count } End Function

34 Dim procs = _ From proc In Process.GetProcesses() _ Select ??? Dim procs= _ From proc In Process.GetProcesses( ) _ Select New ProcInfo With {.ProcessName = proc.ProcessName, _.Count =proc.Threads.Count } Dim procs= _ From proc In Process.GetProcesses( ) _ Select proc.ProcessName, proc.Threads.Count Class ProcInfo Public ProcessName As String Public Count As Integer End Class What goes here?

35 Dim procs= _ From proc In Process.GetProcesses( ) _ Select New With {.ProcessName = proc.ProcessName, _.Count = proc.Threads.Count Dim procs= _ From proc In Process.GetProcesses( ) _ Select proc.ProcessName, proc.Threads.Count Class $Anonymous1 Public ProcessName As String Public Count As Integer End Class Dim bytes = _ From Page In pages _ Select New $Anonymous1 With {.ProcessName = proc.ProcessName, _.Count = proc.Threads.Count } Type omitted from object initializer Anonymous type What is the result type?

36 Defer code interpretation Dim f As Predicate(Of Customer) = Function (c) c.State = "CA" Dim e As Expression(Of Predicate(Of Customer)) = Function (c) c.State = "CA" 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("State") ), New ConstantExpression("CA") ) System.Linq.Expressions.Expression(Of T) where T is a delegate type

37 System.Nullable(Of T) Provides nullability for any value type Structure that combines a T and a Boolean Public Structure Nullable(Of T As Structure) Private value As T Private hasValue As Boolean Public ReadOnly Property Value As T Value … End Property Public ReadOnly Property HasValue As Boolean … End Property End Structure 123 Integer 123 Nullable(Of Integer) True ??? False Non-nullNull

38 T? syntax Nothing unification Conversions Lifted operators Ternary operator Null coalescing Dim x As Integer? = 123 Dim y As Double?= 1.0 Dim x As Integer?= Nothing Dim y As Double?= Nothing Dim i As Integer = 123 Dim x As Integer? x = i ‘ T  T? widening Dim i = CType(x, Integer) ‘ T?  T narrowing Dim x As Integer? = GetNullableInt() Dim y As Integer?= GetNullableInt() Dim z As Integer = x + y ‘ null propagating Dim x As Integer? = GetNullableInt() Dim i As Integer = IF(x, 0) Dim x As Integer? = GetNullableInt() Dim i As Integer = IF(x, x * 2, 0)

39 Optionally provide implementation Partial Class DataContainer Private Partial Sub OnValueChange(value As String) End Sub Public Sub ChangeValue(newValue As String) Me.OnValueChange(newValue) Me._value = newValue End Sub End Class Partial Class DataContainer Private Sub OnValueChanged(value As String) ‘ Code to react to value changing End Sub End Class Typically found in designer generated code Only called if implementation is provided, else No-Op User code provides implementation optionally No method body for declaration

40 Dim custs() As Customer = SampleData.GetCustomers()custsPhoneNameID Dim q = From c In custs Where c.State = "WA“ Select c.City Dim q = custs.Where(Function(c) c.State = “WA”).Select(Function(c) c.City)Select Function(c) c.City Dim names() As String = q.ToArray()names Function(c) c.State= “WA” Where


Download ppt "Paul Yuknewicz Lead Program Manager Microsoft Visual Basic DEV 319."

Similar presentations


Ads by Google