使用LINQ查询非泛型类型

开发 后端
在.NET Framework 2.0 引进泛型之前,都是使用System.Collections namespace命名空间下的集合存储对象。在.NET 2.0中,许多集合类纷纷实现了IEnumerable<T>接口而泛型化。但是,还有相当部分的类没有实现IEnumerable的泛型版 本。

LINQ能实现查询泛型对象或者实现了IEnumerable接口的对象。然而,诸如ArrayList这样的非泛型集合并没有实现IEnumerable接口。接下来,让我们看看,在这种情况下,如何使用LINQ查询非泛型集合。

C#


 1public class Cars
 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}
VB.net
 
 

 1Public Class CarsClass Cars
 2Private privateCarMake As String
 3Public Property CarMake()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()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()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 ProgramClass Program
33Shared Sub Main()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接口导致不能使用LINQ进行查询。那么, 是否我们能妄下结论说LINQ不能查询非泛型集合呢?那么那些返回ArrayList对象的类库将何去何从?我们是否能巧妙地处理这些问题或者使用能返回 泛型集体的方法代替呢?在不改变特性的前提下,让我们看看三个解决问题的小技巧。

方法一,显式指定变量类型,该类型与集合中的对象类型必须一致。


通过显式声明变量类型进行查询,你可以将集合中的每个对象转换成指定的对象。

C#
1    var cc = from Cars car in carList
2             where car.CarMake == "BMW"
3             select car;
VB.net

1      Dim cc = _
2            From car As Cars In carList _
3            Where car.CarMake = "BMW" _
4            Select car
 注意我们将转变量car换成Cars

方法二,使用Cast方法


Cast方法可以将(实现IEnumerable)接口的)非泛型集合返回IEnumerable接口对象。一旦我们得到IEnumerable接口对象,我们就可以使用LINQ查询了。

 

C#
1    var cc1 = from car in carList.Cast()
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#

1    var cc2 = from car in carList.OfType()
2              where car.CarMake == "BMW"
3              select car;

VB.net

1      Dim cc2 = _
2            From car In carList.OfType(Of Cars)() _
3            Where car.CarMake = "BMW" _
4            Select car

3种方法的全部代码如下所示

C#

Code
 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()
53              where car.CarMake == "BMW"
54              select car;
55
56        var cc2 = from car in carList.OfType()
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 QueryArraylistWithLINQNamespace QueryArraylistWithLINQ
 8Public Class CarsClass Cars
 9Private privateCarMake As String
10Public Property CarMake()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()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()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 ProgramClass Program
40      Shared Sub Main()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

【编辑推荐】

  1. 手把手教你用好LINQ to SQL
  2. 使用LINQ和ADO.NET创建Silverlight程序
  3. 详解在ASP.NET中用LINQ实现数据处理
责任编辑:彭凡 来源: cnblogs
相关推荐

2009-09-08 16:36:10

LINQ查询基于泛型类

2009-05-27 10:12:27

LINQ泛型字典Dictionary

2009-09-14 18:57:19

LINQ查询

2009-09-14 14:01:21

LINQ泛型数据集

2010-05-17 09:34:46

LINQAjax

2011-04-13 09:16:55

泛型

2023-12-28 10:30:56

类型系统Rust

2022-06-19 22:54:08

TypeScript泛型工具

2021-09-29 18:17:30

Go泛型语言

2009-08-24 14:51:25

C# 泛型泛型类型

2009-09-08 09:24:50

LINQ查询

2024-10-28 00:40:49

Go语法版本

2024-11-05 09:11:09

TypeScript开发者代码

2013-03-20 09:27:33

C#泛型

2009-01-04 16:58:48

LINQ to XMLLINQXML

2024-01-09 09:27:57

Rust编程泛型

2023-01-05 17:13:28

TypeScript泛型组件

2022-04-15 09:55:59

Go 泛型Go 程序函数

2009-09-01 16:14:11

C#泛型

2024-03-06 08:17:18

Java泛型安全
点赞
收藏

51CTO技术栈公众号