Main idea is to use OfType<T> to cast to generic variant
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using System; using System.Collections.Generic; using System.Collections; // << non generic using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinqAndNonGenericCollections { class Program { static void Main(string[] args) { LinqOverArrayList(); Console.ReadLine(); } static void LinqOverArrayList() { ArrayList cars = new ArrayList() // << non generic { new Car {PetName="BMW",Color="1234",Speed=10,Make=123456 }, new Car {PetName="Lada",Color="1234",Speed=20,Make=123456 }, new Car {PetName="Mitsubishi",Color="1234",Speed=30,Make=123456 }, new Car {PetName="Ferrari",Color="1234",Speed=40,Make=123456 } }; // transform ArrayList in type compatible to IEnumerable<T> var carsEnum = cars.OfType<Car>(); // <<< !!! var fastCars = from c in carsEnum where c.Speed > 20 select c; foreach (var car in fastCars) { Console.WriteLine(car.PetName); } } } class Car { public string PetName { get; set; } public string Color { get; set; } public int Speed { get; set; } public int Make { get; set; } } } |