1
私は車両のリストと空のIEnumerableを持っています。ユーザーがXAML上でラジオボタンを変更すると、VehicleTypeに一致しないリストから特定の車両を削除したいIEnumerableを使ってリストをソートする - XAML
質問:同じ文字列を持たない車両のリストを削除して表示するにはどうすればいいですか?ユーザーによって示さタイプ(プロパティでVehicleType)
コード:
public List<Vehicle> VehicleList = new List<Vehicle>();
public IEnumerable<Vehicle> FilteredVehicleList = new List<Vehicle>();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Vehicle v1 = new Car() { Make = "Ford", Model = "Fiesta", Price = 10000, Year = "1999", Colour = "Red", Mileage = 40000, 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 = 36000, 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 = 14000, 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 = 27000, 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);
listbox_cars.ItemsSource = VehicleList;
string[] filterBox = { "All", "Price", "Mileage", "Make" };
comboBox_sortCars.ItemsSource = filterBox;
comboBox_sortCars.SelectedIndex = 0;
}
private void comboBox_sortCars_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var output = listbox_cars.ItemsSource;
string sortBy = comboBox_sortCars.SelectedValue.ToString();
switch (sortBy)
{
case "All":
output = VehicleList;
break;
case "Make":
FilteredVehicleList = VehicleList.OrderBy(i => i.Make);
output = FilteredVehicleList;
break;
case "Price":
FilteredVehicleList = VehicleList.OrderBy(i => i.Price);
output = FilteredVehicleList;
break;
case "Mileage":
FilteredVehicleList = VehicleList.OrderBy(i => i.Mileage);
output = FilteredVehicleList;
break;
}
ええ、私は文字通りちょうど今、おかげでそれを試みました。 VehicleTypeで過去の車を取り除こうとすると、どうすればいいですか? –
これに 'Where'を使うことができます。例えばOrderBy(i => i.Mileage) 'または単にoutput.Where(i => i.VehicleType.Equals(" Car ")) ) 'スイッチの最後に –
素晴らしい、ありがとう –