**["Получение данных из внешних источников.CSV."](https://github.com/kolei/OAP/blob/master/articles/lab_wpf_data_csv.md)** *** **C#** *** **Class1** *** ``` using System.Collections.Generic; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; namespace WpfApp1.model { public class cars { public string name { get; set; } public int year { get; set; } public double price { get; set; } public string color { get; set; } public bool defects { get; set; } public DateOnly dateOfLastSTO { get; set; } public string photo { get; set; } } } ``` *** **Class2** *** ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApp1.model { class Globals { public static IDataProvider dataProvider; IEnumerable getNamecar() { return new Namecar[] { new Namecar { title = "Toyota Trueno AE8"}, new Namecar { title = "Toyota Supra A80"}, new Namecar { title = "Nissal Skyline R34" }, new Namecar { title = "Nissan Silvia S15"}, new Namecar { title = "Toyota Camry 3.5"}, new Namecar { title = "Audi RS 6" }, new Namecar { title = "Трактор LOVOL TE354 HT"}, new Namecar { title = "BMW M5 F90"}, new Namecar { title = "BMW E36" }, new Namecar { title = "Daewoo Matiz" } }; } IEnumerable getCarsprice() { return new Carsprice[] { new Carsprice{title="Малая цена", priceFrom=0, priceTo=15001}, new Carsprice{title="Средняя цена", priceFrom = 20000, priceTo=35000}, new Carsprice{title="Высокая цена", priceFrom = 35001, priceTo=70000}, }; } } } ``` *** **Class3** *** ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApp1.model { public class Carsprice { public string title { get; set; } public int priceFrom { get; set; } public int priceTo { get; set; } } public class Namecar { public string title { get; set; } } } ``` *** **Class4** *** ``` using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using CsvHelper; namespace WpfApp1.model { class Class4 { public class CSVDataProvider : LocalDataProvider, IDataProvider { private IEnumerable carList; public CSVDataProvider() { using (var reader = new StreamReader("./data.csv")) { using (var csv = new CsvReader( reader, CultureInfo.InvariantCulture)) { carList = csv.GetRecords().ToList(); } } } public IEnumerablegetCars() { return carList; } } } } ``` *** **data.csv** *** ``` name,year,price,color,defects,dateOfLastSTO,photo, Toyota Trueno AE8,1995,15000,white,false,4/30/2024,, Toyota Supra A80,1996,30000,blue,false,5/1/2024,, Nissal Skyline R34,1996,25000,red,true,5/2/2024,, Nissan Silvia S15,1999,20000,green,false,5/3/2024,, Toyota Camry 3.5,2020,20000,black,true,5/4/2024,, Audi RS 6,2016,30000,purple,false,5/5/2024,, Трактор LOVOL TE354 HT,2024,66666,red,false,5/6/2024,, BMW M5 F90,2019,30000,white,true,5/7/2024,, BMW E36,2006,35000,green,true,5/8/2024,, Daewoo Matiz,2010,20000,blue,false,5/9/2024,, ``` *** **MainWindow.xaml.cs** *** ``` using CsvHelper; using System; using System.ComponentModel; using System.Globalization; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using WpfApp1.model; using static WpfApp1.model.Class4; namespace WpfApp1 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window, INotifyPropertyChanged { private string searchFilter = ""; private void SearchFilter_KeyUp(object sender, KeyEventArgs e) { searchFilter = SearchFilterTextBox.Text; Invalidate(); } private bool sortAsc = true; private void RadioButton_Checked(object sender, RoutedEventArgs e) { sortAsc = (sender as RadioButton).Tag.ToString() == "1"; Invalidate(); } public event PropertyChangedEventHandler PropertyChanged; private void Invalidate() { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("CarList")); } public string selectedname = "Все автомобили"; public Carsprice? selectedprice = null; private IEnumerable _CarList; public IEnumerable CarList { get { var res = _CarList; res = res .Where(c => (c.name == selectedname || selectedname == "Все автомобили")) .Where(c => (selectedprice == null || (c.price >= selectedprice.priceFrom && c.price < selectedprice.priceTo))); if (searchFilter != "") res = res.Where(c => c.name.IndexOf( searchFilter, StringComparison.OrdinalIgnoreCase) >= 0); if (sortAsc) res = res.OrderBy(c => c.year); else res = res.OrderByDescending(c => c.year); return res; } set { _CarList = value; } } public List Cars { get; set; } public List CarinfoList { get; set; } public List CarpriceList { get; set; } public MainWindow() { InitializeComponent(); DataContext = this; Globals.dataProvider = new CSVDataProvider(); CarList = Globals.dataProvider.getCars(); CarinfoList = Globals.dataProvider.getName().ToList(); CarinfoList.Insert(0, new Namecar { title = "Все автомобили" }); CarpriceList = Globals.dataProvider.getPrice().ToList(); } private void ExitButton_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); } private void carnameFilterComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { selectedname = (carnameFilterComboBox.SelectedItem as Namecar).title; Invalidate(); } private void carpriceFilterComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { selectedprice = carpriceFilterComboBox.SelectedItem as Carsprice; Invalidate(); } } interface IDataProvider { IEnumerable getCars(); IEnumerable getName(); IEnumerable getPrice(); } public class LocalDataProvider : IDataProvider { public IEnumerable getName() { return new Namecar[] { new Namecar() { title="Toyota Trueno AE86" }, new Namecar() { title="Toyota Supra A80" }, new Namecar() { title="Nissal Skyline R34" }, new Namecar() { title="Nissan Silvia S15" }, new Namecar() { title="Toyota Camry 3.5" }, new Namecar() { title="Audi RS 6" }, new Namecar() { title="Трактор LOVOL TE354 HT" }, new Namecar() { title="BMW M5 F90" }, new Namecar() { title="BMW E36" }, new Namecar() { title="Daewoo Matiz" } }; } public IEnumerable getPrice() { return new Carsprice[] { new Carsprice() { title="Все цены", priceFrom=0, priceTo=70000 }, new Carsprice() { title="Малая цена", priceFrom=0, priceTo=15001 }, new Carsprice() { title="Средняя цена", priceFrom=20000, priceTo=35000 }, new Carsprice() { title="Высокая цена", priceFrom=35001, priceTo=70000 } }; } public IEnumerable getCars() { return new cars[] { new cars { price = 15000, name = "Toyota Trueno AE86", year = 1995, }, new cars { price = 30000, name = "Toyota Supra A80", year = 1996, }, new cars { price = 25000, name = "Nissal Skyline R34", year = 1996 }, new cars { price = 20000, name = "Nissan Silvia S15", year = 1999, }, new cars { price = 20000, name = "Toyota Camry 3.5", year = 2020, }, new cars { price = 30000, name = "Audi RS 6", year = 2016, }, new cars { price = 66666, name = "Трактор LOVOL TE354 HT", year = 2024, }, new cars { price = 30000, name = "BMW M5 F90", year = 2019, }, new cars { price = 35000, name = "BMW E36", year = 2006, }, new cars { price = 20000, name = "Daewoo Matiz", year = 2010, }, }; } } } ``` *** **MainWindow.xaml** *** ```