LINQ能实现查询泛型对象或者实现了IEnumerable
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
方法一,显式指定变量类型,该类型与集合中的对象类型必须一致。
通过显式声明变量类型进行查询,你可以将集合中的每个对象转换成指定的对象。
C# 1 var cc = from Cars car in carList VB.net
|
方法二,使用Cast方法
Cast方法可以将(实现IEnumerable)接口的)非泛型集合返回IEnumerable
C# 1 var cc1 = from car in carList.Cast |
注意,方法一中的所介绍的方法与这种方法相似。
方法三,除了Cast运算符,我们也可以使用OfType
OfType可以过滤集合中相同类型的元素,如果你的集合中包含了不同类型的元素。这个方法得到指定类型的元素对象。
C#
1 var cc2 = from car in carList.OfType |
VB.net
|
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
【编辑推荐】