在.NET Framework 2.0 引进泛型之前,都是使用System.Collections namespace命名空间下的集合存储对象。在.NET 2.0中,许多集合类纷纷实现了IEnumerable<T>接口而泛型化。但是,还有相当部分的类没有实现IEnumerable的泛型版本。
LINQ能实现查询泛型对象或者实现了IEnumerable<T>接口的对象。然而,诸如ArrayList这样的非泛型集合并没有实现IEnumerable<T>接口。接下来,让我们看看,在这种情况下,如何使用LINQ查询非泛型集合。
C#
2Private privateCarMake As String
3Public Property CarMake() As String
4 Get
5 Return privateCarMake
6 End Get
7 Set(ByVal value As String)
8 privateCarMake = value
9 End Set
10End Property
11Private privateCarModel As String
12Public Property CarModel() As String
13 Get
14 Return privateCarModel
15 End Get
16 Set(ByVal value As String)
17 privateCarModel = value
18 End Set
19End Property
20Private privateYear As Integer
21Public Property Year() As Integer
22 Get
23 Return privateYear
24 End Get
25 Set(ByVal value As Integer)
26 privateYear = value
27 End Set
28End Property
29End Class
30
31
32Friend Class Program
33Shared Sub Main(ByVal args() As String)
34 Dim carList As New ArrayList()
35 carList.Add(New Cars With {.CarMake="BMW", .CarModel="BMW Art", .Year=1978})
36 carList.Add(New Cars With {.CarMake = "BMW", .CarModel = "Coupe", .Year = 1982})
37 carList.Add(New Cars With {.CarMake = "Renault", .CarModel = "Alpine", .Year = 1972})
38 carList.Add(New Cars With {.CarMake = "Porsche", .CarModel = "Maisto", .Year = 1976})
39
40 Dim carQuery = _
41 From car In carList _
42 Where car.CarMake = "BMW" _
43 Select car
44End Sub
如上面的代码所示,我们声明Cars对象并将其填充到ArrayList集合当中。并且使用类似LINQ查询泛型集合的方法来查询这个ArrayList集合。你认为结果会如何?
当 然,上面的代码并没有通过编译,原因是ArrayList并没有实现IEnumerable<T>接口导致不能使用LINQ进行查询。那么, 是否我们能妄下结论说LINQ不能查询非泛型集合呢?那么那些返回ArrayList对象的类库将何去何从?我们是否能巧妙地处理这些问题或者使用能返回 泛型集体的方法代替呢?在不改变特性的前提下,让我们看看三个解决问题的小技巧。
方法一,显式指定变量类型,该类型与集合中的对象类型必须一致。
通过显式声明变量类型进行查询,你可以将集合中的每个对象转换成指定的对象。
C# VB.net
2 where car.CarMake == "BMW"
3 select car;
1 Dim cc = _
2 From car As Cars In carList _
3 Where car.CarMake = "BMW" _
4 Select car
注意我们将转变量car换成Cars
方法二,使用Cast方法
Cast方法可以将(实现IEnumerable)接口的)非泛型集合返回IEnumerable<T>接口对象。一旦我们得到IEnumerable<T>接口对象,我们就可以使用LINQ查询了。
C#
1 var cc1 = from car in carList.Cast<Cars>()
2 where car.CarMake == "BMW"
3 select car; VB.net
1 Dim cc1 = _
2 From car In carList.Cast(Of Cars)() _
3 Where car.CarMake = "BMW" _
4 Select car
注意,方法一中的所介绍的方法与这种方法相似。
方法三,除了Cast运算符,我们也可以使用OfType
OfType可以过滤集合中相同类型的元素,如果你的集合中包含了不同类型的元素。这个方法得到指定类型的元素对象。
C#
2 where car.CarMake == "BMW"
3 select car;
2 From car In carList.OfType(Of Cars)() _
3 Where car.CarMake = "BMW" _
4 Select car
3种方法的全部代码如下所示
C#
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Collections;
6
7namespace QueryArraylistWithLINQ
8{
9public class Cars
10{
11public string CarMake { get;set;}
12public string CarModel { get; set; }
13public int Year { get; set; }
14}
15
16
17class Program
18{
19 static void Main(string[] args)
20 {
21 ArrayList carList = new ArrayList();
22 carList.Add(new Cars
23 {
24 CarMake = "BMW",
25 CarModel = "BMW Art",
26 Year = 1978
27 });
28 carList.Add(new Cars
29 {
30 CarMake = "BMW",
31 CarModel = "Coupe",
32 Year = 1982
33 });
34 carList.Add(new Cars
35 {
36 CarMake = "Renault",
37 CarModel = "Alpine",
38 Year = 1972
39 });
40 carList.Add(new Cars
41 {
42 CarMake = "Porsche",
43 CarModel = "Maisto",
44 Year = 1976
45 });
46
47
48 var cc = from Cars car in carList
49 where car.CarMake == "BMW"
50 select car;
51
52 var cc1 = from car in carList.Cast<Cars>()
53 where car.CarMake == "BMW"
54 select car;
55
56 var cc2 = from car in carList.OfType<Cars>()
57 where car.CarMake == "BMW"
58 select car;
59
60
61 foreach (Cars c in cc1)
62 Console.WriteLine(c.CarMake + "-" + c.CarModel);
63
64 Console.ReadLine();
65 }
66}
67}
VB.net
1Imports System
2Imports System.Collections.Generic
3Imports System.Linq
4Imports System.Text
5Imports System.Collections
6
7Namespace QueryArraylistWithLINQ
8Public Class Cars
9Private privateCarMake As String
10Public Property CarMake() As String
11 Get
12 Return privateCarMake
13 End Get
14 Set(ByVal value As String)
15 privateCarMake = value
16 End Set
17End Property
18Private privateCarModel As String
19Public Property CarModel() As String
20 Get
21 Return privateCarModel
22 End Get
23 Set(ByVal value As String)
24 privateCarModel = value
25 End Set
26End Property
27Private privateYear As Integer
28Public Property Year() As Integer
29 Get
30 Return privateYear
31 End Get
32 Set(ByVal value As Integer)
33 privateYear = value
34 End Set
35End Property
36End Class
37
38
39Friend Class Program
40 Shared Sub Main(ByVal args() As String)
41 Dim carList As New ArrayList()
42 carList.Add(New Cars With {.CarMake = "BMW", .CarModel = "BMW Art", .Year = 1978})
43 carList.Add(New Cars With {.CarMake = "BMW", .CarModel = "Coupe", .Year = 1982})
44 carList.Add(New Cars With {.CarMake = "Renault", .CarModel = "Alpine", .Year = 1972})
45 carList.Add(New Cars With {.CarMake = "Porsche", .CarModel = "Maisto", .Year = 1976})
46
47
48 Dim cc = _
49 From car As Cars In carList _
50 Where car.CarMake = "BMW" _
51 Select car
52
53 Dim cc1 = _
54 From car In carList.Cast(Of Cars)() _
55 Where car.CarMake = "BMW" _
56 Select car
57
58 Dim cc2 = _
59 From car In carList.OfType(Of Cars)() _
60 Where car.CarMake = "BMW" _
61 Select car
62
63
64 For Each c As Cars In cc1
65 Console.WriteLine(c.CarMake & "-" & c.CarModel)
66 Next c
67
68 Console.ReadLine()
69 End Sub
70End Class
71End Namespace
2{
3public string CarMake { get;set;}
4public string CarModel { get; set; }
5public int Year { get; set; }
6}
7
8
9class Program
10{
11static void Main(string[] args)
12{
13 ArrayList carList = new ArrayList();
14 carList.Add(new Cars
15 {
16 CarMake="BMW", CarModel="BMW Art", Year=1978
17 });
18 carList.Add(new Cars
19 {
20 CarMake = "BMW", CarModel = "Coupe", Year = 1982
21 });
22 carList.Add(new Cars
23 {
24 CarMake = "Renault", CarModel = "Alpine", Year = 1972
25 });
26 carList.Add(new Cars
27 {
28 CarMake = "Porsche", CarModel = "Maisto", Year = 1976
29 });
30
31 var carQuery = from car in carList
32 where car.CarMake == "BMW"
33 select car;
34}