1 / 39

Dev221 VB 中的 LINQ: 未来之路

Dev221 VB 中的 LINQ: 未来之路. 课程内容概述. LINQ 的字面含义是“语言集成的查询” - Language INtegrated Query 。它是下一版 Visual Studio 的功能重点之一。它扩展了 VB ,让大家可以使用类似 SQL 的语言来操作多个数据源,例如 Object Collection, XML, Dataset 以及 Database 。. 课程内容安排. LINQ 项目概述 基于对象数据的 LINQ 基于 SQL 的 LINQ(DLinq) 基于 XML 的 LINQ(XLinq) . Visual Basic 9.0.

jaser
Download Presentation

Dev221 VB 中的 LINQ: 未来之路

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 简化查询数据 在VB语言中集成查询操作 对对象数据、关系型数据和XML数据建立统一的查询 便于使用XML 快速生成XML文档 简化性、一致性&提高生产效率

  5. Language Integrated Query

  6. LINQ项目 <book> <title/> <author/> <year/> <price/> </book> Objects SQL XML VB C# Others… .NET Language Integrated Query StandardQueryOperators DLinq(ADO.NET) XLinq (System.XML)

  7. LINQ的创新点 对对象数据、关系型数据和XML数据建立统一的查询和转换 VB查询语法类似于SQL和Xquery 查询的类型检查、intellisense和代码重构 语言/API设计具有可扩展性

  8. 语言的创新点 Implicitly typed locals 隐式类型的局部变量 Extension methods Extension方法 Inline functions 内联函数 Object initializers 对象初始值 Anonymous types 匿名类型 Query expressions 查询表达式 Dim x = 5 <Extension> Sub Randomize(col As Collection) Function(c) c.Name New Point { x := 1, y := 2 } New { c.Name, c.Phone } From … Where … Select

  9. 查询语法

  10. 关系型数据的DLinq 当前访问数据的方法 SqlConnection c = new SqlConnection(…); c.Open(); SqlCommand cmd = new SqlCommand( @"SELECT c.Name, c.Phone FROM Customers c WHERE c.City = @p0"); cmd.Parameters["@p0"] = "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 No compile time checks

  11. 关系型数据的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 Integrated query syntax Strongly typed results

  12. 编写DLinq应用程序

  13. 关系型数据的DLinq 使用语言中的数据访问 影射tables/rows 到类/对象 基于ADO.NET 以及.NET Transactions 影射 工具生成或手工编写 其它 可以使用存储过程 支持其实数据库 (需插件)

  14. 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 <contacts> <contact> <name>Great Lakes Food</name> <phone>(503) 555-7123</phone> </contact> … </contacts>

  15. XML数据的XLinq 使用XLinq进行编程 Declarative model Dim contacts AsNew XElement("contacts", _ From c in customers _ Where c.Country = "USA“ _ Select New XElement("contact", _ new XElement("name", c.CompanyName), _ new XElement("phone", c.Phone) _ ) ) Elementcentric Integrated queries Smaller and faster

  16. 在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 Expression holes for computed values

  17. 使用XLinq

  18. XML数据的XLinq 与语言集成的XML查询 具有XPath / XQuery的表达能力 将VB或C#作为编程语言 XML和代码之间无任何概念上的障碍 利用DOM的经验 元素为中心,而不是文档为中心 对称的元素/属性API 功能性的构造 文本结点仅仅是字符串 简化的XML命名空间支持 更快且更小

  19. LINQ项目 .NET的Language Integrated Query 在VB9.0和C#3.0的语言中具有内建的查询语法 标准查询操作 对任意.NET集合的类似SQL的查询 DLinq 可查询的数据访问框架 XLinq 可查询的XML DOM, 并且更小, 更快

  20. 总结 Language Integrated Query Language 的优点 对于对象数据,关系型数据和XML数据的统一查询 查询的类型 在VB和C#中的类似SQL和XQuery的查询 仅支持.NET平台 更多信息 主页: http://msdn.microsoft.com/netframework/future/linq/ 可下载Customer Tech Previews版本! Contact: Juliapa@microsoft.com 我们需要您的反馈意见

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

  22. 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 = CDbl(cosmos.@version) Dim item As XElement = cosmos…<item>.Value Dim created As Date = CDate(item.<linkcreated>)

  23. 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. Deferred Query Execution 向后优先解析的查询表达式 custs names Where Select ID Name Phone AddressOf _Filter1 AddressOf _Project1 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()

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

  26. 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())

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

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

  29. 填反馈表

  30. 讲师的Chalk Talk和其他Session

  31. 与本次主题有关的Session和活动

More Related