LINQ概述-通用和便利的信息查询方式

DEMO CODE:http://files.cnblogs.com/rippleyong/LinqDemo.7z

PPT:http://files.cnblogs.com/rippleyong/linq.ppt

 

简介

 

l 面向对象 (OO) 编程技术.
二十年之后,业界在面向对象 (OO) 编程技术的发展过程中趋于稳定。现在,程序员已经认为诸如类、对象和方法等特性是理所当然的。在探究当前的和下一代技术时,明显可以看出,有关编程技术的下一个难题是降低访问和集成特定信息(这些信息不是使用 OO 技术进行原始定义的)的复杂性。非 OO 信息的两个最常见源是关系数据库和 XML

l .NET Language Integrated Query (LINQ):采用通用方案来解决各种信息源的访问与整合问题
对于 LINQ 项目,我们采取了更为普通的方法,并向 .NET Framework 中添加了适用于所有信息源(而不只是关系数据或 XML 数据)的通用查询工具,而不是在编程语言和运行库中添加相关功能或特定于 XML 的功能。该工具名为 .NET 语言集成查询 (LINQ)。

l 语言集成查询使得查询表达式 能够得益于丰富的元数据、编译时语法检查、静态输入和智能感知
我们使用语言集成查询 这一术语表明,该查询是开发人员主要编程语言(例如,C#、Visual Basic)的集成功能。语言集成查询使得查询表达式 能够得益于丰富的元数据、编译时语法检查、静态输入和智能感知(以前只能用于命令代码)。语言集成查询还允许将单个通用的声明查询工具应用于所有内存中信息,而不只是来自外部源的信息

l 标准查询操作符,允许在任何基于 .NET 的编程语言中通过直接的声明方式进行遍历、筛选和投影等操作
共有两组 LINQ 标准查询运算符,一组在类型为 IEnumerable(Of (T)) 的对象上运行(内存中集合上运行),返回的可枚举对象将捕获传递到方法的参数;另一组在类型为 IQueryable(Of (T))的对象上运行,扩展 IQueryable<(Of <(T>)>) 的方法不会实现任何查询行为,但会生成一个表示要执行的查询的表达式目录树

Language Integrated Query

clip_image003
LINQ to Objects是指直接对任意 IEnumerableIEnumerable(Of (T)) 集合使用 LINQ 查询,无需使用中间 LINQ 提供程序或 API,如 LINQ to SQLLINQ to XML。可以使用 LINQ 来查询任何可枚举的集合,如 List(Of (T))ArrayDictionary(Of (TKey, TValue))。该集合可以是用户定义的集合,也可以是 .NET Framework API 返回的集合。
LINQ to XML提供使用 .NET 语言集成查询 (LINQ) Framework 的内存中 XML 编程接口。LINQ to XML 使用最新的 .NET Framework 语言功能,相当于更新的和重新设计的文档对象模型 (DOM) XML 编程接口。
LINQ to ADO.NET:您可以在 ADO.NET 中使用 语言集成查询 (LINQ) 编程模型查询任何可枚举对象

一个简单的LINQ表达式

Dim  numbers  =   New   Integer () { 5 4 1 3 9 8 6 7 2 0 }
Dim  lowNums  =  From n  In  numbers _
Where n 
<   5  _
Select  n

标准查询运算符

筛选数据: Where,OfType
投影运算: Select, SelectMany
对数据进行排序: OrderBy, ThenBy
数据分区: GroupBy
限定符运算: Any, All
数据分区: Take, Skip, TakeWhile, SkipWhile
Set 操作: Distinct, Union, Intersect, Except
聚合运算: Count, Sum, Min, Max, Average
转换数据类型: ToArray, ToList, ToDictionary

DEMO1 LINQ To Objects
Public   Sub  LinqToObjects()

Dim  words  =   New   String () { " hello " " linq " " world " " beautiful " " wonderful " }

Dim  shortWords  =  From word  In  words _

Where word.Length 
>   5  _

Select  word

For   Each  word  In  shortWords

Console.WriteLine(word)

Next

Dim  groups  =  From word  In  words _

Order By word Ascending _

Group By word.Length Into lengthGroups 
=  Group _

Order By words.Length Descending

For   Each  group  In  groups

Console.WriteLine(
String .Format( " Words of length {0} " , group.Length.ToString))

For   Each  word  In  group.lengthGroups

Console.WriteLine(
"   "   +  word)

Next

Next

End Sub

VB9-隐式类型本地变量(Local Type Inference )

 

' Object Initializers
Dim  user1  =   New  User()  With  {.Age  =   8 , .FirstName  =   " Yong " , .LastName  =   " Kong " }
Dim  user2  =   New  User( " Kong " " Kong " With  {.Age  =   8 }

 

VB9-对象初始值设置项(Object Initializers)

 

 

Public   Class  User

Public  FirstName  As   String

Public  LastName  As   String

Public  Age  As   Integer

Public   Sub   New ( ByVal  lastName  As   String )

_LastName 
=  lastName

End Sub

End Class

Dim  user  =   New  User  With  _

{.Age 
=   30 , .FirstName  =   " Yong " , .LastName  =   " Kong " }

Dim  user2  =   New  User( " Kong " With  _

{.Age 
=   8 , .FirstName  =   " Yong}

 

VB9-匿名类型(Anonymous Types)

 

' Anonymous Types

Dim  user4  =   New   With  {.FirstName  =   " Yong " , .LastName  =   " Kong " }

 

VB9-扩展方法(Extension Mehtods)

 

< System.Runtime.CompilerServices.Extension() >  _

Module  UserHelper

< System.Runtime.CompilerServices.Extension() >  _

Public   Function  GetUserName( ByVal  user  As  User)  As   String

Return   String .Format( " your Name:{0} {1} " , _° user.FirstName ,user.LastName)°  End Function

End Module

 

参数它表明了扩展方法与类之间的关系,这个参数是什么类型,这个方法就会被扩展到对应类型的实例上.

 

Dim  user  =   New  User( " Kong " " Kong " With  {.Age  =   8 }

Dim  userName  =  user.GetUserName()

 

参数:调用方法的参数总比扩展方法的参数少1个,因为扩展方法的第1个参数,用于确定扩展方法与要扩展的类之间的关系

VB9-Lambda表达式(Lambda Expressions)

简单的Lambda表达式

 

Dim  lambda1  =   Function (x) x  +   1   '  Implicitly typed

Dim  lambda2  =   Function (x  As   Integer ) x  +   1   '  Explicitly typed

Dim  lambda3  =   Function (x, y) x  *  y  '  Multiple parameters

Dim  lambda4  =   Function ()  1   '  No parameters

 

泛型委托

 

Dim  upperImplicit  As  Func( Of   String String =   Function (s) s.ToUpper()

 

LINQ To SQL-介绍

l LINQ to SQL 是 .NET Framework 3.5 版的一个组件,提供了用于将关系数据作为对象管理的运行时基础结构。

l 在 LINQ to SQL 中,关系数据库的数据模型映射到用开发人员所用的编程语言表示的对象模型。

l 当应用程序运行时,LINQ to SQL 会将对象模型中的语言集成查询转换为 SQL,然后将它们发送到数据库进行执行。当数据库返回结果时,LINQ to SQL 会将它们转换回您可以用您自己的编程语言处理的对象

LINQ To SQL-重要概念

l 数据上下文(DataContext)

– System.Data.Linq命名空间下的重要类型,用于把查询句法翻译成SQL语句,以及把数据从数据库返回给调用方和把实体的修改写入数据库

– DataContext提供了以下一些使用的功能:

u 以日志形式记录DataContext生成的SQL

u 执行SQL(包括查询和更新语句)

u 创建和删除数据库

– DataContext是实体和数据库之间的桥梁

l 实体类(Entity Classes)

DEMO 2 Linq To SQL

Public   Sub  LINQToSQL()

Dim  db  As   New  NorthwindDataContext

Dim  query  =  From c  In  db.Customers _

Where c.City 
=   " Paris "  _

Select  c

For   Each  customer  In  query

Console.WriteLine(customer.CustomerName)

Next

Console.WriteLine(
" ------------------- " )

Dim  query2  =  From c  In  db.Customers _

Where c.City 
=   " Paris "  _

Select  ( New   With  {Key .CustomerName  =  c.CustomerName, Key .companyName  =  c.ContactName})

For   Each  customer  In  query2

Console.WriteLine(customer)

Next

Console.WriteLine(
" ------------------- " )

Dim  sql  As   String   =   " SELECT * FROM Customers WHERE City='Paris' "

Dim  query3  =  db.ExecuteQuery( Of  Customer)(sql)

For   Each  customerX  In  query3

Console.WriteLine(customerX.CustomerName)

Next

Dim  cmd  As  Data.Common.DbCommand  =  db.GetCommand(query3)

' Console.WriteLine("reader-------------------")

' Dim sqlreader As String = "SELECT * FROM Customers WHERE City='Paris'"

' Dim cmd = New SqlClient.SqlCommand(sqlreader)

' Dim reader = cmd.ExecuteReader

' Dim query4 = db.Translate(Of Customer)(reader)

' For Each customerX In query4

'  Console.WriteLine(customerX.CustomerName)

' Next

End Sub

LINQ To XML-介绍

l LINQ to XML 经过了重新设计,是最新的 XML 编程方法。

l 它提供文档对象模型 (DOM) 的内存文档修改功能,支持 LINQ 查询表达式。 尽管这些查询表达式在语法上与 XPath 不同,但它们以更加类型化的方式提供类似的功能。

LINQ To XML-重要概念

l XDocument 类

– 包含有效的 XML 文档所需的信息。其中包括 XML 声明、处理指令和注释

l XElement 类

– 表示一个 XML 元素。 可以使用该类创建元素;更改元素内容;添加、更改或删除子元素;向元素中添加属性;或以文本格式序列化元素内容。

l XAttribute 类

– 属性是与元素关联的名称/值对。 XAttribute 类表示 XML 属性。

DEMO 3 LINQ To XML

Public   Sub  LINQTOXML()

Dim  books()  =  { _

New   With  {.Title  =   " Ajax in Action " , .Publisher  =   " Manning " , .Year  =   2005 }, _

New   With  {.Title  =   " Windows Forms in Action " , .Publisher  =   " Manning " , .Year  =   2006 }, _

New   With  {.Title  =   " RSS and Atom in Action " , .Publisher  =   " Manning " , .Year  =   2006 } _

}

'  Build the XML fragment based on the collection

Dim  xml  As  XElement  =   New  XElement( " books " , _

From book 
In  books _

Where book.Year 
=   2006  _

Select   New  XElement( " book " , _

New  XAttribute( " title " , book.Title), _

New  XElement( " publisher " , book.Publisher) _

) _

)

'  Build an XML fragment using XML literals

Dim  xml2  As  XElement  =  _

< books >

< % =  From book  In  books _

Where book.Year 
=   2006  _

Select  _

< book title =< % =  book.Title % >>

< publisher >< % =  book.Publisher % ></ publisher >

</ book >  _

%
>

</ books >

'  Dump the XML to the console

Console.WriteLine(xml)

Console.WriteLine(
" ---------------- " )

Console.WriteLine(xml2)

End Sub

LINQ学习工具

l LINQPad :LINQPad 是一个很好的学习LINQ的工具,LINQPad 是完全免费的,无需安装, 《C# 3.0 in a Nutshell》一书中200个示例

l VLinq :Visual Linq Query Builder(LINQ可视化查询编辑器)作为Visual Studio 2008的一个插件,可以帮助我们在程序中创建LINQ to SQL查询表达式,支持C#和VB两种语言

LINQ资源

l LINQ in Action 电子书:作者:Fabrice Marguerie, Steve Eichert, Jim Wooley 网址: http://linqinaction.net

l The LINQ Project(LINQ 项目)站点位于 MSDN 上,提供了有关 LINQ 的一般新闻和信息。

l Visual Basic Developer Center(Visual Basic 开发人员中心),提供了有关结合使用 LINQ 和 Visual Basic 的最新新闻。

l C# Developer Center(C# 开发人员中心),提供了有关 C# 3.0 规范的最新版本及其他信息。

l LINQ forum(LINQ 论坛)位于 MSDN 上,是提出有关 LINQ 的问题的最佳地方。

l 白皮书提供了指向提供有关 LINQ 的其他信息的白皮书的链接。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值