VB9.0新特性之LINQ(二) - 常用关键字
2008-07-08 08:45
281 查看
在VB9.0新特性之LNIQ(一)中,我们了解到了LINQ查询的三个关键步骤。LINQ查询好比是一顿丰盛的晚餐:买菜(确定数据源)、做菜(创建查询)、进餐(执行查询)。毋庸置疑,做菜做得怎么样,直接决定进餐的质量。而LINQ的关键字就像是做菜时候的油盐酱醋——道道菜单少不了,不同的菜还要不同的组合下不同的量。
题外扯一句:写惯SQL语句的人一提及查询,就必然想到SELECT……在LINQ中,SELECT不是必须的,而且,它也往往出现在查询语句的结尾部分。谁让现代人都流行把SQL倒着写呢 :-)
好,下面进入正题。来看看做菜常常会用到哪些调料——哦,不对,是LINQ中有哪些常用的关键字 :-)
我们仍然借用在VB9.0新特性之LNIQ(一)中创建的“假的”数据库——nbStorage为例,来进行各种各样五花八门的查询:
'laptop collection act as a db
2 Dim nbStorage = New Notebook() {New Notebook With {.Brand = "Lenovo", .Price = 16000, .Type = "T61", .Weight = 2.3} _
3 , New Notebook With {.Brand = "HP", .Price = 8000, .Type = "V3742TU", .Weight = 2.5} _
4 , New Notebook With {.Brand = "HP", .Price = 5399, .Type = "HP520", .Weight = 2.4} _
5 , New Notebook With {.Brand = "DELL", .Price = 8900, .Type = "D630", .Weight = 2.6}}
如果您对Notebook类有疑问,请参看《VB 9.0新特性之局部类型推理(Local Type Inference)》中Notebook类(后文对此类有细微修改,但大体骨架未动过)的定义。
菜1:查询出所有结果:
1 'Select data from db
2 Console.WriteLine("All Laptops")
3 Dim allLaptops = From lapTop In nbStorage
4 For Each aLaptop In allLaptops
5 Console.WriteLine(aLaptop.ToString())
6 Next
7 'Output
8 'All(Laptops)
9 'Laptop Lenovo(T61) is 2.3kg, and at the price of 16000
10 'Laptop HP(V3742TU) is 2.5kg, and at the price of 8000
11 'Laptop HP(HP520) is 2.4kg, and at the price of 5399
12 'Laptop DELL(D630) is 2.6kg, and at the price of 8900
第3行代码就是“调料”所在了,新关键字From引出了一个LINQ。根据局部类型推理,allLaptops为一个结果集,但由于它的延迟绑定特性,此时,它相当于只有一个定义,并没有真正的获得数据。From后面的lapTop对象,类型是nbStorage的元素的类型,其作用相当于为后面的获取数据提供一个访问器;In后面指定了查询的数据源:nbStorage。
因此说,LINQ中第一个关键字是From,而不是Select。From表明:这是一个LINQ,并且指定了查询的数据源。
菜2:选出所有价格大8000元(含)的笔记本:
1'Where price > =8000
2 Console.WriteLine(vbCrLf + "Where price > =8000")
3 Dim laptops2 = From laptop In nbStorage Where laptop.Price >= 8000
4 For Each aLaptop In laptops2
5 Console.WriteLine(aLaptop.ToString())
6 Next
7 'Output
8 'Where(price >= 8000)
9 'Laptop Lenovo(T61) is 2.3kg, and at the price of 16000
10 'Laptop HP(V3742TU) is 2.5kg, and at the price of 8000
11 'Laptop DELL(D630) is 2.6kg, and at the price of 8900
第3行代码,我们又多了一剂调味品:Where。Where用来带出筛选条件。LINQ依据条件,筛选出合格的数据。在这里,我们设定了查询筛选条件:laptop.price >= 8000,于是把所有price大于等于8000的笔记本给筛选了出现。
Where后面还可以添加组合条件,例如laptop.Price >= 8000 and laptop.weight <= 2.5。
菜3:排序输出结果
1 'Order by Price Descending
2 Console.WriteLine(vbCrLf + "Order by Price Descending")
3 Dim laptops3 = From laptop In nbStorage _
4 Where laptop.Price >= 8000 _
5 Order By laptop.Price Descending
6 For Each aLaptop In laptops3
7 Console.WriteLine(aLaptop.ToString())
8 Next
9 'Output
10 'Order by Price Descending
11 'Laptop Lenovo(T61) is 2.3kg, and at the price of 16000
12 'Laptop DELL(D630) is 2.6kg, and at the price of 8900
13 'Laptop HP(V3742TU) is 2.5kg, and at the price of 8000
在这个查询里,第3行由From关键字引出查询,然后一直写到了第5行。新关键大家一看就明白了:Order By ... Descending。Order By后面写上按照哪一列来排序。加上Descending是为了让结果从降序(从大到小)排序,如果是要升序排序,可以用关键字:Order By ... Ascending或者干脆只写一个Order By。
菜4:去粗取精,选择部分列:
1 'Select some of the fields
2 Console.WriteLine(vbCrLf & "Select Some of The Fields")
3 Dim laptops4 = From laptop In nbStorage _
4 Where laptop.Price >= 8000 _
5 Order By laptop.Price Descending _
6 Select laptop.Brand, laptop.Price
7
8 For Each aLaptop In laptops4
9 Console.WriteLine("{0} salse in {1}", aLaptop.Brand, aLaptop.Price)
10 Next
11 'Output
12 'Select Some of The Fields
13 'Lenovo salse in 16000
14 'DELL salse in 8900
15 'HP salse in 8000
第3行,怎么又是第3行?:-)一直到第6行,形成了一个LINQ。这里,我们终于见到了久违的关键字Select。通过Select关键字,选择出了.Brand和.Price列,其它信息一概不要。所以说,Select关键字用于筛选符合条件的列;离散数学中对这种操作有一个比较形象的称呼:映射。
这个LINQ与前面所述的LINQ有一个很大的区别:laptops4的类型,它一定是一个结果集没错。但是它的元素到底是什么类型的呢?是不是仍然是Notebook类?或者是其它什么类型?嘿嘿,这里先卖个大关子。
菜5:去除重复的结果
1 'Select Distinct
2 Console.WriteLine(vbCrLf & "Uising Distinct")
3 Dim laptops5 = From laptop In nbStorage _
4 Select Brand = laptop.Brand Distinct
5 For Each aLaptop In laptops5
6 Console.WriteLine(aLaptop)
7 Next
8 'Output:
9 'Uising Distinct
10 'Lenovo
11 'HP
12 'DELL
嗯,LINQ依旧是从第3行开始,新关键字躲在了Select的后面——Distinct。 Distinct去除结果中重复的记录。在这个LINQ中,如果不加Distinct,应该选择到四个结果。但由于结果中存在2个HP,因此,最终结果只列出了三行。
小结一下本文中出现的关键字:
From:引出LINQ,指定数据源;
Where :筛选出符合条件的行;
Order By :排序,和Ascending、Descending配合使用;
Select:选择符合条件的列;
Distinct:筛去重复的记录。
有了这些“调味品”,要做出大而全的满汗全席恐怕还有点儿困难,但做个日常食用的家常小炒不成问题。
其实对于有SQL基础的朋友,介绍这些关键字实在有些多余,大家只要把代码一复制、粘贴,运行起来看看,注意一下应用的顺序和不断出现的局部类型推理,相信很快,LINQ就能上手了。
题外扯一句:写惯SQL语句的人一提及查询,就必然想到SELECT……在LINQ中,SELECT不是必须的,而且,它也往往出现在查询语句的结尾部分。谁让现代人都流行把SQL倒着写呢 :-)
好,下面进入正题。来看看做菜常常会用到哪些调料——哦,不对,是LINQ中有哪些常用的关键字 :-)
我们仍然借用在VB9.0新特性之LNIQ(一)中创建的“假的”数据库——nbStorage为例,来进行各种各样五花八门的查询:
'laptop collection act as a db
2 Dim nbStorage = New Notebook() {New Notebook With {.Brand = "Lenovo", .Price = 16000, .Type = "T61", .Weight = 2.3} _
3 , New Notebook With {.Brand = "HP", .Price = 8000, .Type = "V3742TU", .Weight = 2.5} _
4 , New Notebook With {.Brand = "HP", .Price = 5399, .Type = "HP520", .Weight = 2.4} _
5 , New Notebook With {.Brand = "DELL", .Price = 8900, .Type = "D630", .Weight = 2.6}}
如果您对Notebook类有疑问,请参看《VB 9.0新特性之局部类型推理(Local Type Inference)》中Notebook类(后文对此类有细微修改,但大体骨架未动过)的定义。
菜1:查询出所有结果:
1 'Select data from db
2 Console.WriteLine("All Laptops")
3 Dim allLaptops = From lapTop In nbStorage
4 For Each aLaptop In allLaptops
5 Console.WriteLine(aLaptop.ToString())
6 Next
7 'Output
8 'All(Laptops)
9 'Laptop Lenovo(T61) is 2.3kg, and at the price of 16000
10 'Laptop HP(V3742TU) is 2.5kg, and at the price of 8000
11 'Laptop HP(HP520) is 2.4kg, and at the price of 5399
12 'Laptop DELL(D630) is 2.6kg, and at the price of 8900
第3行代码就是“调料”所在了,新关键字From引出了一个LINQ。根据局部类型推理,allLaptops为一个结果集,但由于它的延迟绑定特性,此时,它相当于只有一个定义,并没有真正的获得数据。From后面的lapTop对象,类型是nbStorage的元素的类型,其作用相当于为后面的获取数据提供一个访问器;In后面指定了查询的数据源:nbStorage。
因此说,LINQ中第一个关键字是From,而不是Select。From表明:这是一个LINQ,并且指定了查询的数据源。
菜2:选出所有价格大8000元(含)的笔记本:
1'Where price > =8000
2 Console.WriteLine(vbCrLf + "Where price > =8000")
3 Dim laptops2 = From laptop In nbStorage Where laptop.Price >= 8000
4 For Each aLaptop In laptops2
5 Console.WriteLine(aLaptop.ToString())
6 Next
7 'Output
8 'Where(price >= 8000)
9 'Laptop Lenovo(T61) is 2.3kg, and at the price of 16000
10 'Laptop HP(V3742TU) is 2.5kg, and at the price of 8000
11 'Laptop DELL(D630) is 2.6kg, and at the price of 8900
第3行代码,我们又多了一剂调味品:Where。Where用来带出筛选条件。LINQ依据条件,筛选出合格的数据。在这里,我们设定了查询筛选条件:laptop.price >= 8000,于是把所有price大于等于8000的笔记本给筛选了出现。
Where后面还可以添加组合条件,例如laptop.Price >= 8000 and laptop.weight <= 2.5。
菜3:排序输出结果
1 'Order by Price Descending
2 Console.WriteLine(vbCrLf + "Order by Price Descending")
3 Dim laptops3 = From laptop In nbStorage _
4 Where laptop.Price >= 8000 _
5 Order By laptop.Price Descending
6 For Each aLaptop In laptops3
7 Console.WriteLine(aLaptop.ToString())
8 Next
9 'Output
10 'Order by Price Descending
11 'Laptop Lenovo(T61) is 2.3kg, and at the price of 16000
12 'Laptop DELL(D630) is 2.6kg, and at the price of 8900
13 'Laptop HP(V3742TU) is 2.5kg, and at the price of 8000
在这个查询里,第3行由From关键字引出查询,然后一直写到了第5行。新关键大家一看就明白了:Order By ... Descending。Order By后面写上按照哪一列来排序。加上Descending是为了让结果从降序(从大到小)排序,如果是要升序排序,可以用关键字:Order By ... Ascending或者干脆只写一个Order By。
菜4:去粗取精,选择部分列:
1 'Select some of the fields
2 Console.WriteLine(vbCrLf & "Select Some of The Fields")
3 Dim laptops4 = From laptop In nbStorage _
4 Where laptop.Price >= 8000 _
5 Order By laptop.Price Descending _
6 Select laptop.Brand, laptop.Price
7
8 For Each aLaptop In laptops4
9 Console.WriteLine("{0} salse in {1}", aLaptop.Brand, aLaptop.Price)
10 Next
11 'Output
12 'Select Some of The Fields
13 'Lenovo salse in 16000
14 'DELL salse in 8900
15 'HP salse in 8000
第3行,怎么又是第3行?:-)一直到第6行,形成了一个LINQ。这里,我们终于见到了久违的关键字Select。通过Select关键字,选择出了.Brand和.Price列,其它信息一概不要。所以说,Select关键字用于筛选符合条件的列;离散数学中对这种操作有一个比较形象的称呼:映射。
这个LINQ与前面所述的LINQ有一个很大的区别:laptops4的类型,它一定是一个结果集没错。但是它的元素到底是什么类型的呢?是不是仍然是Notebook类?或者是其它什么类型?嘿嘿,这里先卖个大关子。
菜5:去除重复的结果
1 'Select Distinct
2 Console.WriteLine(vbCrLf & "Uising Distinct")
3 Dim laptops5 = From laptop In nbStorage _
4 Select Brand = laptop.Brand Distinct
5 For Each aLaptop In laptops5
6 Console.WriteLine(aLaptop)
7 Next
8 'Output:
9 'Uising Distinct
10 'Lenovo
11 'HP
12 'DELL
嗯,LINQ依旧是从第3行开始,新关键字躲在了Select的后面——Distinct。 Distinct去除结果中重复的记录。在这个LINQ中,如果不加Distinct,应该选择到四个结果。但由于结果中存在2个HP,因此,最终结果只列出了三行。
小结一下本文中出现的关键字:
From:引出LINQ,指定数据源;
Where :筛选出符合条件的行;
Order By :排序,和Ascending、Descending配合使用;
Select:选择符合条件的列;
Distinct:筛去重复的记录。
有了这些“调味品”,要做出大而全的满汗全席恐怕还有点儿困难,但做个日常食用的家常小炒不成问题。
其实对于有SQL基础的朋友,介绍这些关键字实在有些多余,大家只要把代码一复制、粘贴,运行起来看看,注意一下应用的顺序和不断出现的局部类型推理,相信很快,LINQ就能上手了。
相关文章推荐
- VB9.0新特性之LINQ(六)——1对N
- 微软免费图书《Introducing Microsoft LINQ》翻译Chapter3.2-和C#相似的VB9.0的语法特性
- VB 9.0新特性之LINQ(一)
- VB9.0新特性之LINQ(三) - Join和Group
- VB9.0新特性之LINQ(五) - 数据库生成的类
- VB9.0新特性之LINQ(七)——增、删、改操作
- VB9.0新特性之LINQ(四) - LINQ to SQL
- VB 9.0新特性之局部类型推理(Local Type Inference)
- VB 9.0新特性之对象初始化器(Object Initializers)(一)
- VB 9.0新特性之对象初始化器(Object Initializers)(二)
- VB 9.0新特性之Lambda表达式(Lambda Expressions)
- 微软免费图书《Introducing Microsoft LINQ》翻译Chapter3.1-Visual Basic 9.0语言特性
- 委托是什么?匿名方法是什么?在C# 3.0中,Lambda表达式是什么?扩展方法是什么?LINQ是什么?您觉得C# 3.0中还有哪些重要的特性,它们带来了什么优势?BCL中哪些类库和这些特性有关?您平时最常用哪些
- (40)面向对象的三大特性继承性(41)面向对象中常用的关键字和魔术方法
- Visual Studio 2008开发新特性系列课程(2):C#3.0与VB9.0的新特性介绍
- VB 9.0新特性之扩展方法(Extension Mehtods)
- Linq基础之常用关键字
- excel宏(VB)常用保留字及关键字整理
- Visual Studio 2008开发新特性系列课程(2):C#3.0与VB9.0的新特性介绍
- linq的常用写法