程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 使用LINQ查詢非泛型類型

使用LINQ查詢非泛型類型

編輯:關於.NET

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.net1      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.net1      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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved