using System; using System.Collections.Generic; using Avalonia.Controls; using AvaloniaApplication5.Models; using AvaloniaApplication5.Views; using Microsoft.EntityFrameworkCore; using System.Linq; using ReactiveUI; namespace AvaloniaApplication5.ViewModels { public class PageUserListViewModel : ReactiveObject { _1234Context DBConnect; public PageUserListViewModel(_1234Context dBConnect) { this.DBConnect = dBConnect; userList = DBConnect.Logintables.Include(x => x.IdRoleNavigation).Include(x => x.User.IdGenderNavigation).ToList(); } List userList; public List UserList { get => userList; set => this.RaiseAndSetIfChanged(ref userList, value); } public List OrdersName => new List() { "По возрастанию", "По убыванию" }; string orderByName = "По возрастанию"; public string OrderByName { get => orderByName; set { orderByName = value; AllFilters(); } } public List Genders//список полов для фильтра { get { List newlist = new List() { "Любой" }; newlist.AddRange(DBConnect.Genders.Select(x => x.Gender1).ToList()); return newlist; } } string filterByGender = "Любой"; public string FilterByGender { get => filterByGender; set { filterByGender = value; AllFilters(); } } string namefind; public string Namefind { get => namefind; set { namefind = value; AllFilters(); } } void AllFilters() { //всегда перед сортировкой получаем новый лист userList = DBConnect.Logintables.Include(x => x.IdRoleNavigation).Include(x => x.User.IdGenderNavigation).ToList(); //поиск по имени if (!(namefind == "" || namefind == null)) UserList = UserList.Where(x => x.User.Name.Contains(namefind)).ToList(); //сортировка по возрастанию имени switch (orderByName) { case "По возрастанию": UserList = UserList.OrderBy(x => x.User.Name).ToList(); break; case "По убыванию": UserList = UserList.OrderByDescending(x => x.User.Name).ToList(); break; } //фильтр по полу if (!(filterByGender == "Любой" || filterByGender == null)) UserList = UserList.Where(x => x.User.IdGenderNavigation.Gender1 == filterByGender).ToList(); } } }