Download presentation
Presentation is loading. Please wait.
1
Dev221 VB中的LINQ:未来之路
2
课程内容概述 LINQ的字面含义是“语言集成的查询”- Language INtegrated Query。它是下一版Visual Studio的功能重点之一。它扩展了VB,让大家可以使用类似SQL的语言来操作多个数据源,例如Object Collection, XML, Dataset以及Database。
3
课程内容安排 LINQ项目概述 基于对象数据的LINQ 基于SQL的LINQ(DLinq) 基于XML的LINQ(XLinq)
4
Visual Basic 9.0 简化查询数据 便于使用XML 简化性、一致性&提高生产效率 在VB语言中集成查询操作
2019年1月16日7时32分 Visual Basic 9.0 简化查询数据 在VB语言中集成查询操作 对对象数据、关系型数据和XML数据建立统一的查询 便于使用XML 快速生成XML文档 简化性、一致性&提高生产效率 Visual Basic 9.0 Goals Visual Basic 2005 is going to be a really exciting release with tons of great features, but as we look beyond November, we’ve been thinking about how we can make a major improvements in the ease and speed with which people program in Visual Basic. Specifically, we’ve been looking at two areas for the next major release. (Click.) We’ve been thinking about how we can simplify querying data. Today, when you query data, you tend to run into two problems. One problem is that you often have to end up writing a lot of code over and over and over again to do what are relatively simple and straightforward operations – sort, filter, project, etc. The other problem is that you frequently have to deal with a huge amount of friction between the .NET type system and other type systems such as SQL and XML that make querying those types of data more difficult than it should be. Finally, we’ve been thinking about how we can make it easier and simpler to work with XML. With the increasingly central role that XML plays in the programming world, producing XML is becoming more and more a common programming task, and one that still requires too much boilerplate code. What we’re going to talk about today, then, are some of the cool solutions that we’ve come up with for these three problem areas that we think will make programming in Visual Basic (and C#) radically easier and faster. 4 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
5
Language Integrated Query
2019年1月16日7时32分 Language Integrated Query Demo: Basic Language Integrated Query over In-Memory objects 5 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
6
LINQ项目 .NET Language Integrated Query VB C# Others…
2019年1月16日7时32分 LINQ项目 VB C# Others… .NET Language Integrated Query Standard Query Operators DLinq (ADO.NET) XLinq (System.XML) Project LINQ Language integrated query is actually a platform-wide effort. The core is a set of API patterns that define how an object exposes query operations. Languages build features on top of those APIs. Components expose query services through those APIs. Both sides are fully documented and extensible. SQL Objects <book> <title/> <author/> <year/> <price/> </book> XML 6 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
7
LINQ的创新点 对对象数据、关系型数据和XML数据建立统一的 查询和转换 VB查询语法类似于SQL和Xquery
2019年1月16日7时32分 LINQ的创新点 对对象数据、关系型数据和XML数据建立统一的 查询和转换 VB查询语法类似于SQL和Xquery 查询的类型检查、intellisense和代码重构 语言/API设计具有可扩展性 7 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
8
语言的创新点 Implicitly typed locals 隐式类型的局部变量 Extension methods Extension方法
2019年1月16日7时32分 语言的创新点 Dim x = 5 Implicitly typed locals 隐式类型的局部变量 Extension methods Extension方法 Inline functions 内联函数 Object initializers 对象初始值 Anonymous types 匿名类型 Query expressions 查询表达式 <Extension> Sub Randomize(col As Collection) Function(c) c.Name New Point { x := 1, y := 2 } New { c.Name, c.Phone } 100% backwards compatible From … Where … Select 8 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
9
查询语法 Project Select <expr> Filter Where <expr> Test
2019年1月16日7时32分 查询语法 Project Select <expr> Filter Where <expr> Test Any(<expr>), All(<expr>) Join <expr> Join <expr> On <expr> Group Group By <expr> Aggregate Count(<expr>), Sum(<expr>), Min(<expr>), Max(<expr>), Avg(<expr>) Partition Top <expr> Set Distinct, Union, Intersect, Except Order Order By <expr> Convert ToArray, ToList, ToBindingList Cast As <typename>, OfType(Of T) 9 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
10
关系型数据的DLinq 当前访问数据的方法 SqlConnection c = new SqlConnection(…);
2019年1月16日7时32分 关系型数据的DLinq 当前访问数据的方法 SqlConnection c = new SqlConnection(…); c.Open(); SqlCommand cmd = new SqlCommand( @"SELECT c.Name, c.Phone FROM Customers c WHERE c.City = "London"; DataReader dr = c.Execute(cmd); while (dr.Read()) { string name = r.GetString(0); string phone = r.GetString(1); DateTime date = r.GetDateTime(2); } r.Close(); Queries in quotes Loosely bound arguments Loosely typed result sets Goal: show how powerful, but cumbersome ADO.NET is today No compile time checks 10 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
11
关系型数据的DLinq 使用DLing访问数据 Classes describe data Public Class Customer …
2019年1月16日7时32分 关系型数据的DLinq 使用DLing访问数据 Classes describe data Public Class Customer … Public Class Northwind Inherits DataContext Public Property Customers As Table(Of Customer) … End Class Tables are like collections Strongly typed connection Dim db As New Northwind(…) Dim contacts = _ From c in db.Customers _ Where c.City = "London" Select c.Name, c.Phone Goal: showcase DLinq simplicity and how it solves previously identified problems Integrated query syntax Strongly typed results 11 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
12
编写DLinq应用程序 2019年1月16日7时32分 Part I
Goal: showcase the designer and the power of the query language Remove object model Create new DLinq designer object Drag and drop Customer, Order, Order detail and show properties Re-execute same query Query for both customers and nested orders (it is better than SQL!!) Part II Goal: showcase Joins (how to ask questions about classes that don’t have an explicit relationship) Drag and drop Supplier Join Customer/Supplier over Country, return c.CustomerID, c.Country, Sups.Count() Execute Add a where clause Part IV Goal: show the debugging of the SQL query Place a breakpoint Use the DLinq visualizer to inspect the SQL query In the visualizer Change the select to get City 12 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
13
关系型数据的DLinq 使用语言中的数据访问 影射 其它 影射tables/rows 到类/对象
2019年1月16日7时32分 关系型数据的DLinq 使用语言中的数据访问 影射tables/rows 到类/对象 基于ADO.NET 以及.NET Transactions 影射 工具生成或手工编写 其它 可以使用存储过程 支持其实数据库 (需插件) Goal: summary of demo 13 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
14
XML数据的XLinq 当前XML编程方式 Imperative model Dim doc As New XmlDocument()
2019年1月16日7时32分 XML数据的XLinq 当前XML编程方式 Imperative model 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) Document centric No integrated queries Memory intensive Goal: highlight imperative DOM model <contacts> <contact> <name>Great Lakes Food</name> <phone>(503) </phone> </contact> … </contacts> 14 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
15
XML数据的XLinq 使用XLinq进行编程 Declarative model
2019年1月16日7时32分 XML数据的XLinq 使用XLinq进行编程 Declarative model Dim contacts As 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) _ ) Element centric Integrated queries Goal: highlight declarative DOM model Smaller and faster 15 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
16
Expression holes for computed values
2019年1月16日7时32分 在Visual Basic中集成XML 利用XLinq在VB中进行XML编程 Infers XLinq XElement Dim contacts = _ <contacts> <%= _ From c In customers _ Where c.Country = "USA" _ Select <contact> <name><%= c.CompanyName %></name> <phone><%= c.Phone %></phone> </contact> _ %> </contacts> No conceptual barrier True to VB’s design principals: - Best practices by default – Translates to XLinq - Intuitive – No conceptual barrier between Object Model and XML you intend to write - Productive – Full IDE integration, colorization, Intellisense, - Pragmatic – Expression holes for inserting captured values Expression holes for computed values 16 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
17
2019年1月16日7时32分 使用XLinq Part I Goal: show the declarative simplicity of creating an XML doc Iteratively create a simple XML doc by using the constructor semantic (i.e. Grandaddy, Daddy, Son) Part II Goal: show how working across data domain is greatly simplified with LINQ Create and XML doc that gets filled by a query that goes against northwind Part III Goal: showcase VB XML integration Show XElement Show XML syntax Show cut and paste Show accessing member 17 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
18
XML数据的XLinq 与语言集成的XML查询 具有XPath / XQuery的表达能力 利用DOM的经验 元素为中心,而不是文档为中心
2019年1月16日7时32分 XML数据的XLinq 与语言集成的XML查询 具有XPath / XQuery的表达能力 将VB或C#作为编程语言 XML和代码之间无任何概念上的障碍 利用DOM的经验 元素为中心,而不是文档为中心 对称的元素/属性API 功能性的构造 文本结点仅仅是字符串 简化的XML命名空间支持 更快且更小 Goal: summary of demo 18 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
19
LINQ项目 .NET的Language Integrated Query 标准查询操作 DLinq XLinq
2019年1月16日7时32分 LINQ项目 .NET的Language Integrated Query 在VB9.0和C#3.0的语言中具有内建的查询语法 标准查询操作 对任意.NET集合的类似SQL的查询 DLinq 可查询的数据访问框架 XLinq 可查询的XML DOM, 并且更小, 更快 Goal: this is what you should remember of the presentation 19 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
20
总结 Language Integrated Query Language 的优点 更多信息
2019年1月16日7时32分 总结 Language Integrated Query Language 的优点 对于对象数据,关系型数据和XML数据的统一查询 查询的类型 在VB和C#中的类似SQL和XQuery的查询 仅支持.NET平台 更多信息 主页: 可下载Customer Tech Previews版本! Contact: 我们需要您的反馈意见 20 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
21
© 2005 Microsoft Corporation. All rights reserved.
2019年1月16日7时32分 © 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary. 21 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
23
XML Member Binding XML成员绑定
Late binding covers all XML axes 延后绑定所有的XML axes Dim cosmos As XElement = GetCurrentLinkCosmos().Root Dim ver As Double = CDbl(cosmos.Attribute("version")) Dim item As XElement = First(cosmos.Descendents("item")) Dim created As Date = CDate(item.Element("linkcreated“)) Attributes Descendents Elements Dim cosmos As XElement = GetCurrentLinkCosmos().Root Dim ver As Double = Dim item As XElement = cosmos…<item>.Value Dim created As Date = CDate(item.<linkcreated>)
24
XML Literals XML Literals
2019年1月16日7时32分 XML Literals XML Literals Shorthand for object creation 编写对象生成代码 Dim emp = _ <employee> <name>Joe</name> <age>28</age> <department id="432"> <deptname>Engineering</deptname> </department> </employee> Dim emp = _ New XElement("employee", _ New XElement("name", "Joe"), _ New XElement("age", 28), _ New XElement("department", _ New XElement("name", "Engineering"), _ New XAttribute("id", 432))) 24 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
25
Deferred Query Execution 向后优先解析的查询表达式
2019年1月16日7时32分 Deferred Query Execution 向后优先解析的查询表达式 Dim custs() As Customer = SampleData.GetCustomers() Dim q = Select c.City From c In custs Where c.State = "WA" Dim q = custs.Where(AddressOf _Filter1).Select(AddressOf _Project1) Dim names() As String = q.ToArray() custs Phone Name ID names AddressOf _Filter1 Where Select AddressOf _Project1 Deferred Query Execution 25 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
26
Expression Trees 表达式树 Defer code interpretation 向后优先解析的代码
System.Query.Expression(Of T) where T is a delegate type Dim f As Predicate(Of Customer) = (c) c.State = "CA" Dim e As Expression(Of Predicate(Of Customer)) = (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") )
27
Extension Methods 扩展模型
Extend existing types with new methods 使用新方法扩展存在的类型 Extension IntArrExtensions To Integer() Function Sort() As Integer() … End Function ReadOnly Property Sum() As Integer Get End Get End Property End Extension obj.Foo(x, y) XXX.Foo(obj, x, y) Imports IntegerArrExtensions Dim values() As Integer = GetValues() Console.WriteLine(IntegerArrExtensions.Sum(IntegerExtensions.Sort(values)) Dim values() As Integer = GetValues() Console.WriteLine(values.Sort().Sum())
28
Simple Type Inference 简单类型引用
2019年1月16日7时32分 Simple Type Inference 简单类型引用 Variable type inferred from initializer 由初始值推断的变量类型 Integer All types are Object! String Dim x = 5 Dim s = "Hello" Dim d = 1.0 Dim numbers = New Integer() {1, 2, 3} Dim orders = new Dictionary(Of Integer, Order)() Double Add at the end that type inferencing has some compat implicaitons... But we think it's okay. 28 ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
35
Add link to external Community website
List top 3 newsgroups related to this slide 1 2 3 Advise when your next chat is Next user group meeting you will be at Add Other related 3rd party sites
36
填反馈表
37
讲师的Chalk Talk和其他Session
38
与本次主题有关的Session和活动
Similar presentations