# Фильтрация данных ## Фильтрация по словарю ``` using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; namespace WpfApp2 { public partial class MainWindow : Window { private List instruments; private Dictionary> filters; public MainWindow() { InitializeComponent(); InitializeData(); foreach (var filter in filters.Keys) { FilterComboBox.Items.Add(filter); } UpdateInstrumentList(instruments); } private void InitializeData() { instruments = new List { new MusicalInstrument { Name = "Гитара", Type = "Струнные", Price = 300 }, new MusicalInstrument { Name = "Барабаны", Type = "Ударные", Price = 500 }, new MusicalInstrument { Name = "Фортепиано", Type = "Клавишные", Price = 1000 }, new MusicalInstrument { Name = "Скрипка", Type = "Струнные", Price = 400 }, new MusicalInstrument { Name = "Труба", Type = "Медные", Price = 600 },, }; filters = new Dictionary> { { "Все", i => true }, { "Струнные", i => i.Type == "Струнные" }, { "Ударные", i => i.Type == "Ударные" }, { "Клавишные", i => i.Type == "Клавишные" }, { "Медные", i => i.Type == "Медные" }, { "Деревянные", i => i.Type == "Деревянные" }, { "До 500", i => i.Price <= 500 }, { "От 500 до 1000", i => i.Price > 500 && i.Price <= 1000 }, { "Более 1000", i => i.Price > 1000 } }; } private void FilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { string selectedFilter = (string)FilterComboBox.SelectedItem; if (filters.TryGetValue(selectedFilter, out Func filterFunc)) { List filteredInstruments = instruments.Where(filterFunc).ToList(); UpdateInstrumentList(filteredInstruments); } } private void UpdateInstrumentList(List instrumentsToDisplay) { InstrumentListView.ItemsSource = instrumentsToDisplay; } } class MusicalInstrument { public string Name { get; set; } public string Type { get; set; } public double Price { get; set; } } } ``` ``` ``` ![](./img/Снимок экрана 2024-04-29 023952.png) ![](./img/Снимок экрана 2024-04-29 024155.png) ## Фильтрация по условию ``` using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; namespace WpfApp2 { public partial class MainWindow : Window { private List instruments; public MainWindow() { InitializeComponent(); InitializeData(); FilterComboBox.Items.Add("Все"); FilterComboBox.Items.Add("Клавишные"); FilterComboBox.Items.Add("Струнные"); FilterComboBox.Items.Add("Ударные"); FilterComboBox.SelectedIndex = 0; UpdateInstrumentList(instruments); } private void InitializeData() { instruments = new List { new MusicalInstrument { Name = "Гитара", Type = "Струнные", Price = 300 }, new MusicalInstrument { Name = "Барабаны", Type = "Ударные", Price = 500 }, new MusicalInstrument { Name = "Фортепиано", Type = "Клавишные", Price = 1000 }, new MusicalInstrument { Name = "Скрипка", Type = "Струнные", Price = 400 }, new MusicalInstrument { Name = "Труба", Type = "Медные", Price = 600 }, }; } private void FilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { string selectedFilter = (string)FilterComboBox.SelectedItem; List filteredInstruments = new List(); switch (selectedFilter) { case "Все": filteredInstruments = instruments; break; case "Клавишные": filteredInstruments = instruments.Where(i => i.Type == "Клавишные").ToList(); break; case "Струнные": filteredInstruments = instruments.Where(i => i.Type == "Струнные").ToList(); break; case "Ударные": filteredInstruments = instruments.Where(i => i.Type == "Ударные").ToList(); break; } UpdateInstrumentList(filteredInstruments); } private void UpdateInstrumentList(List instrumentsToDisplay) { InstrumentListView.ItemsSource = instrumentsToDisplay; } } class MusicalInstrument { public string Name { get; set; } public string Type { get; set; } public string Brand { get; set; } public double Price { get; set; } } } ``` ``` ``` ![](./img/Снимок экрана 2024-04-29 030342.png) ![](./img/Снимок экрана 2024-04-29 030355.png)