PageUserListViewModel.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Controls;
  4. using AvaloniaApplication5.Models;
  5. using AvaloniaApplication5.Views;
  6. using Microsoft.EntityFrameworkCore;
  7. using System.Linq;
  8. using ReactiveUI;
  9. namespace AvaloniaApplication5.ViewModels
  10. {
  11. public class PageUserListViewModel : ReactiveObject
  12. {
  13. _1234Context DBConnect;
  14. public PageUserListViewModel(_1234Context dBConnect)
  15. {
  16. this.DBConnect = dBConnect;
  17. userList = DBConnect.Logintables.Include(x => x.IdRoleNavigation).Include(x => x.User.IdGenderNavigation).ToList();
  18. }
  19. List<Logintable> userList;
  20. public List<Logintable> UserList { get => userList; set => this.RaiseAndSetIfChanged(ref userList, value); }
  21. public List<string> OrdersName => new List<string>() { "Ïî âîçðàñòàíèþ", "Ïî óáûâàíèþ" };
  22. string orderByName = "Ïî âîçðàñòàíèþ";
  23. public string OrderByName
  24. {
  25. get => orderByName;
  26. set
  27. {
  28. orderByName = value;
  29. AllFilters();
  30. }
  31. }
  32. public List<string> Genders//ñïèñîê ïîëîâ äëÿ ôèëüòðà
  33. {
  34. get
  35. {
  36. List<string> newlist = new List<string>() { "Ëþáîé" };
  37. newlist.AddRange(DBConnect.Genders.Select(x => x.Gender1).ToList());
  38. return newlist;
  39. }
  40. }
  41. string filterByGender = "Ëþáîé";
  42. public string FilterByGender
  43. {
  44. get => filterByGender;
  45. set
  46. {
  47. filterByGender = value;
  48. AllFilters();
  49. }
  50. }
  51. string namefind;
  52. public string Namefind { get => namefind; set { namefind = value; AllFilters(); } }
  53. void AllFilters()
  54. {
  55. //âñåãäà ïåðåä ñîðòèðîâêîé ïîëó÷àåì íîâûé ëèñò
  56. userList = DBConnect.Logintables.Include(x => x.IdRoleNavigation).Include(x => x.User.IdGenderNavigation).ToList();
  57. //ïîèñê ïî èìåíè
  58. if (!(namefind == "" || namefind == null))
  59. UserList = UserList.Where(x => x.User.Name.Contains(namefind)).ToList();
  60. //ñîðòèðîâêà ïî âîçðàñòàíèþ èìåíè
  61. switch (orderByName)
  62. {
  63. case "Ïî âîçðàñòàíèþ":
  64. UserList = UserList.OrderBy(x => x.User.Name).ToList();
  65. break;
  66. case "Ïî óáûâàíèþ":
  67. UserList = UserList.OrderByDescending(x => x.User.Name).ToList();
  68. break;
  69. }
  70. //ôèëüòð ïî ïîëó
  71. if (!(filterByGender == "Ëþáîé" || filterByGender == null))
  72. UserList = UserList.Where(x => x.User.IdGenderNavigation.Gender1 == filterByGender).ToList();
  73. }
  74. }
  75. }