Dev 221 VBLINQ Language Integrated Query LINQ VB
Dev 221 VB中的LINQ: 未来之路
Language Integrated Query
LINQ项目 VB C# Others… . NET Language Integrated Query Standard Query Operators DLinq (ADO. NET) XLinq (System. XML) <book> <title/> <author/> <year/> <price/> </book> Objects SQL XML
语言的创新点 Implicitly typed locals 隐式类型的局部变量 Extension methods Extension方法 Dim x = 5 <Extension> Sub Randomize(col As Collection) Function(c) c. Name Inline functions 内联函数 New Point { x : = 1, y : = 2 } Object initializers 对象初始值 New { c. Name, c. Phone } Anonymous types 匿名类型 Query expressions 查询表达式 From … Where … Select
查询语法 Project Select <expr> Filter Where <expr> Test Any(<expr>), All(<expr>) Join <expr> On <expr> Group By <expr> Aggregate Count(<expr>), Sum(<expr>), Min(<expr>), Max(<expr>), Avg(<expr>) Partition Top <expr> Set Distinct, Union, Intersect, Except Order By <expr> Convert To. Array, To. List, To. Binding. List Cast As <typename>, Of. Type(Of T)
关系型数据的DLinq 当前访问数据的方法 Sql. Connection c = new Sql. Connection(…); Queries in c. Open(); quotes Sql. Command cmd = new Sql. Command( @"SELECT c. Name, c. Phone FROM Customers c Loosely bound WHERE c. City = @p 0"); arguments cmd. Parameters["@p 0"] = "London"; Data. Reader dr = c. Execute(cmd); while (dr. Read()) { Loosely typed string name = r. Get. String(0); result sets string phone = r. Get. String(1); Date. Time date = r. Get. Date. Time(2); } r. Close(); No compile time checks
关系型数据的DLinq 使用DLing访问数据 Public Class Customer … Classes describe data Public Class Northwind Inherits Data. Context Tables are like collections Public Property Customers As Table(Of Customer) … Strongly typed End Class connection Dim db As New Northwind(…) Dim contacts = _ Integrated From c in db. Customers _ query syntax Where c. City = "London" Select c. Name, c. Phone Strongly typed results
XML数据的XLinq 当前XML编程方式 Imperative model Dim doc As New Xml. Document() Dim contacts As XMLElement = doc. Create. Element("contacts") Document For Each Dim c in Customers centric If (c. Country = "USA") Dim e As XMLElement = doc. Create. Element("contact") Dim name As XMLElement = doc. Create. Element("name")No integrated queries name. Inner. Text = c. Company. Name e. Append. Child(name) Dim phone As XMLElement = doc. Create. Element("phone") Memory phone. Inner. Text = c. Phone intensive e. Append. Child(phone) <contacts> <contact> contacts. Append. Child(e) <name>Great Lakes Food</name> End If <phone>(503) 555 -7123</phone> Next </contact> … doc. Append. Child(contacts) </contacts>
XML数据的XLinq 使用XLinq进行编程 Dim contacts As New XElement("contacts", _ From c in customers _ Where c. Country = "USA“ _ Select New XElement("contact", _ new XElement("name", c. Company. Name), _ new XElement("phone", c. Phone) _ ) ) Declarative model Element centric Integrated queries Smaller and faster
在Visual Basic中集成XML 利用XLinq在VB中进行XML编程 Infers XLinq XElement Dim contacts = _ <contacts> No conceptual <%= _ barrier From c In customers _ Where c. Country = "USA" _ Select <contact> <name> <%= c. Company. Name %></name> <phone> <%= c. Phone %></phone> </contact> _ %> </contacts> Expression holes for computed values
使用XLinq
总结 § Language Integrated Query Language 的优点 § 对于对象数据, 关系型数据和XML数据的统一查询 § 查询的类型 § 在VB和C#中的类似SQL和XQuery的查询 § 仅支持. NET平台 § 更多信息 § 主页: http: //msdn. microsoft. com/netframework/future/linq/ § 可下载Customer Tech Previews版本! § Contact: Juliapa@microsoft. com 我们需要您的反馈意见
© 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
XML Member Binding XML成员绑定 Late binding covers all XML axes 延后绑定所有的 XML axes Dim cosmos As XElement = Get. Current. Link. Cosmos(). 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 Dim cosmos As XElement = Get. Current. Link. Cosmos(). Root Elements Dim ver As Double = CDbl(cosmos. @version) Dim item As XElement = cosmos…<item>. Value Dim created As Date = CDate(item. <linkcreated>)
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)))
Deferred Query Execution 向后优先解析的查询表达式 Dim custs() As Customer = Sample. Data. Get. Customers() Dim q = Select c. City From c In custs Where c. State = "WA" Dim q = custs. Where(Address. Of _Filter 1). Select(Address. Of _Project 1) Dim names() As String = q. To. Array() custs ID names Name Phone Where Address. Of _Filter 1 Select Address. Of _Project 1
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)) = _ New Expression(Of Predicate(Of Customer))( New Binary. Expression(Expression. Type. EQ, New Property. Expression( New Parameter. Expression(0), Get. Type(Customer). Get. Property("State") ), New Constant. Expression("CA") ) )
Extension Methods 扩展模型 Extend existing types with new methods 使用新方法扩展存在的类型 Extension Int. Arr. Extensions To Integer() Function Sort() As Integer() … End Function Read. Only Property Sum() As Integer obj. Foo(x, y) Get Imports Integer. Arr. Extensions … XXX. Foo(obj, x, y) End Get Dim values() As Integer = Get. Values() End Property Console. Write. Line(Integer. Arr. Extensions. Sum(Integer. Extensions. Sort(values)) End Extension Dim values() As Integer = Get. Values() Console. Write. Line(values. Sort(). Sum())
Simple Type Inference 简单类型引用 Variable type inferred from initializer 由初始值推断的变量类型 Integer String Double All types are Object! Dim x = 5 Dim s = "Hello" Dim d = 1. 0 Dim numbers = New Integer() {1, 2, 3} Dim orders = new Dictionary(Of Integer, Order)()
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 3 rd party sites
讲师的Chalk Talk和其他Session
- Slides: 39