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}