我有一个名为VehicleList的列表和一个IEnumerable列表调用FileredVehicleList。我使用类及其属性填充VehicleList并将它们放入列表框中。当用户将选项从全部切换到任何选项时,我希望列表框显示仅包含该类车辆的车辆。
所以我的问题是如何获取VehicleList,对其进行排序,仅取出用户想要的车辆并将其放入FilteredVehicleList以显示在列表框中?
代码:
public List<Vehicle> VehicleList = new List<Vehicle>();
public IEnumerable<Vehicle> FilteredVehicleList = new List<Vehicle>();
Vehicle v1 = new Car() { Make = "Ford", Model = "Fiesta", Price = 10000, Year = "1999", Colour = "Red", Mileage = 50000, Description = "Lovely red car, 4 wheel, optional steering wheel.", VehicleType = "Car"};
VehicleList.Add(v1);
Vehicle v2 = new Car() { Make = "Ford", Model = "Mondeo", Price = 20000, Year = "2001", Colour = "Green", Mileage = 50000, Description = "Lovely green car, 4 wheel, optional steering wheel.", VehicleType = "Car" };
VehicleList.Add(v2);
Vehicle v3 = new Car() { Make = "Ford", Model = "Fiesta", Price = 15000, Year = "2003", Colour = "Silver", Mileage = 50000, Description = "Lovely silver car, 4 wheel, optional steering wheel.", VehicleType = "Car" };
VehicleList.Add(v3);
Vehicle v4 = new Car() { Make = "Opel", Model = "Astra", Price = 25000, Year = "2002", Colour = "Silver", Mileage = 50000, Description = "Lovely silver car, 4 wheel, optional steering wheel.", VehicleType = "Car" };
VehicleList.Add(v4);
Vehicle v5 = new Bike() { Make = "Kawasaki", Model = "ZX", Price = 30000, Year = "2008", Colour = "Black", Mileage = 25000, Description = "Lovely black bike, 2 wheel, no optional steering wheel.", VehicleType = "Bike" };
VehicleList.Add(v5);
string[] filterBox = { "All", "Price", "Mileage", "Make" };
comboBox_sortCars.ItemsSource = filterBox;
comboBox_sortCars.SelectedIndex = 0;
FilteredVehicleList = VehicleList;
private void comboBox_sortCars_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var output = listbox_cars.ItemsSource = "";
var filter;
string sortBy = comboBox_sortCars.SelectedValue.ToString();
switch (sortBy)
{
case "Make":
filter = FilteredVehicleList.VehicleType.OrderBy(i => i.Make)
break;
}