| | | 1 | | namespace Services.FiltersServices |
| | | 2 | | { |
| | | 3 | | public class DataFilter<T> : IDataFilter<T> |
| | | 4 | | { |
| | | 5 | | public IEnumerable<T> FilterByInt( |
| | | 6 | | IEnumerable<T> data, |
| | | 7 | | Func<T, int> propertySelector, |
| | | 8 | | int? filterValue) |
| | | 9 | | { |
| | 31 | 10 | | if (filterValue != null) |
| | 76 | 11 | | return data.Where(item => propertySelector(item) == filterValue); |
| | | 12 | | else |
| | | 13 | | { |
| | 1 | 14 | | return data; |
| | | 15 | | } |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public IEnumerable<T> FilterByDate( |
| | | 19 | | IEnumerable<T> data, |
| | | 20 | | Func<T, DateTime?> propertySelector, |
| | | 21 | | DateTime? filterDate) |
| | | 22 | | { |
| | 4 | 23 | | if (filterDate != null) |
| | | 24 | | { |
| | 3 | 25 | | DateTime filterDateDay = filterDate.Value.Date; |
| | 11 | 26 | | return data.Where(item => propertySelector(item)?.Date == filterDateDay); |
| | | 27 | | } |
| | | 28 | | else |
| | | 29 | | { |
| | 1 | 30 | | return data; |
| | | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | |
| | | 35 | | public IEnumerable<T> FilterByString( |
| | | 36 | | IEnumerable<T> data, |
| | | 37 | | Func<T, string?> propertySelector, |
| | | 38 | | string? filterValue, |
| | | 39 | | bool exactMatch = false) |
| | | 40 | | { |
| | 821 | 41 | | if (!string.IsNullOrEmpty(filterValue)) |
| | | 42 | | { |
| | 300 | 43 | | if (exactMatch) |
| | | 44 | | { |
| | 5 | 45 | | return data.Where(item => propertySelector(item) != null && |
| | 5 | 46 | | string.Equals(propertySelector(item), filterValue, StringComparison.Ordina |
| | | 47 | | } |
| | | 48 | | else |
| | | 49 | | { |
| | 299 | 50 | | return data.Where(item => |
| | 299 | 51 | | { |
| | 1730 | 52 | | var value = propertySelector(item); |
| | 1730 | 53 | | return value != null && |
| | 1730 | 54 | | value.IndexOf(filterValue, StringComparison.OrdinalIgnoreCase) >= 0; |
| | 299 | 55 | | }); |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | else |
| | | 59 | | { |
| | 521 | 60 | | return data; |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | |
| | | 65 | | |
| | | 66 | | public IEnumerable<T> FilterByDecimal( |
| | | 67 | | IEnumerable<T> data, |
| | | 68 | | Func<T, decimal?> propertySelector, |
| | | 69 | | decimal? filterValue, |
| | | 70 | | decimal tolerance = 1) |
| | | 71 | | { |
| | 90 | 72 | | if (filterValue != null) |
| | | 73 | | { |
| | 71 | 74 | | return data.Where(item => propertySelector(item) is decimal d && Math.Abs(d - filterValue.Value) <= tole |
| | | 75 | | } |
| | | 76 | | else |
| | | 77 | | { |
| | 76 | 78 | | return data; |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | |
| | | 83 | | public IEnumerable<T> FilterByDouble( |
| | | 84 | | IEnumerable<T> data, |
| | | 85 | | Func<T, double?> propertySelector, |
| | | 86 | | double? filterValue, |
| | | 87 | | double tolerance = 1) |
| | | 88 | | { |
| | 91 | 89 | | if (filterValue != null) |
| | | 90 | | { |
| | 90 | 91 | | return data.Where(item => propertySelector(item) is double d && Math.Abs(d - filterValue.Value) <= toler |
| | | 92 | | } |
| | | 93 | | else |
| | | 94 | | { |
| | 74 | 95 | | return data; |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | |
| | | 100 | | |
| | | 101 | | public IEnumerable<T> FilterByPeriod( |
| | | 102 | | IEnumerable<T> data, |
| | | 103 | | Func<T, DateTime> propertySelector, |
| | | 104 | | DateTime? startDate, |
| | | 105 | | DateTime? endDate) |
| | | 106 | | { |
| | 4 | 107 | | if (startDate != null && endDate != null) |
| | | 108 | | { |
| | 6 | 109 | | return data.Where(item => propertySelector(item) >= startDate && propertySelector(item) <= endDate); |
| | | 110 | | } |
| | 3 | 111 | | else if (startDate != null) |
| | | 112 | | { |
| | 4 | 113 | | return data.Where(item => propertySelector(item) >= startDate); |
| | | 114 | | } |
| | 2 | 115 | | else if (endDate != null) |
| | | 116 | | { |
| | 4 | 117 | | return data.Where(item => propertySelector(item) <= endDate); |
| | | 118 | | } |
| | | 119 | | else |
| | | 120 | | { |
| | 1 | 121 | | return data; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | public IEnumerable<TItem> FilterByEnum<TItem, TEnum>( |
| | | 126 | | IEnumerable<TItem> data, |
| | | 127 | | Func<TItem, TEnum?> propertySelector, |
| | | 128 | | TEnum? filterValue) |
| | | 129 | | where TEnum : struct, Enum |
| | | 130 | | { |
| | 122 | 131 | | if (filterValue != null) |
| | | 132 | | { |
| | 17 | 133 | | return data.Where(item => |
| | 65 | 134 | | propertySelector(item) != null && |
| | 65 | 135 | | propertySelector(item).Equals(filterValue)); |
| | | 136 | | } |
| | | 137 | | else |
| | | 138 | | { |
| | 105 | 139 | | return data; |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | public IEnumerable<T> FilterByTimeSpan( |
| | | 144 | | IEnumerable<T> data, |
| | | 145 | | Func<T, TimeSpan?> propertySelector, |
| | | 146 | | TimeSpan? filterTimeSpan) |
| | | 147 | | { |
| | 2 | 148 | | if (filterTimeSpan != null) |
| | | 149 | | { |
| | 1 | 150 | | return data.Where(item => |
| | 7 | 151 | | propertySelector(item) is TimeSpan ts && |
| | 7 | 152 | | Math.Abs(ts.TotalSeconds - filterTimeSpan.Value.TotalSeconds) <= 1); |
| | | 153 | | } |
| | | 154 | | else |
| | | 155 | | { |
| | 1 | 156 | | return data; |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Фильтрует данные по списку строк с использованием логики OR (элемент должен соответствовать хотя бы одному з |
| | | 162 | | /// </summary> |
| | | 163 | | /// <param name="data">Исходные данные для фильтрации</param> |
| | | 164 | | /// <param name="propertySelector">Селектор свойства для фильтрации</param> |
| | | 165 | | /// <param name="filterValues">Список значений для фильтрации</param> |
| | | 166 | | /// <param name="exactMatch">Если true - точное совпадение, если false - частичное (Contains)</param> |
| | | 167 | | /// <returns>Отфильтрованные данные</returns> |
| | | 168 | | public IEnumerable<T> FilterByStringList( |
| | | 169 | | IEnumerable<T> data, |
| | | 170 | | Func<T, string?> propertySelector, |
| | | 171 | | IEnumerable<string> filterValues, |
| | | 172 | | bool exactMatch = false) |
| | | 173 | | { |
| | | 174 | | // Если список пустой или null - возвращаем данные без фильтрации |
| | 6 | 175 | | if (filterValues == null || !filterValues.Any()) |
| | | 176 | | { |
| | 2 | 177 | | return data; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | // Убираем пустые и null значения из списка |
| | 14 | 181 | | var nonEmptyValues = filterValues.Where(v => !string.IsNullOrEmpty(v)).ToList(); |
| | | 182 | | |
| | 4 | 183 | | if (!nonEmptyValues.Any()) |
| | | 184 | | { |
| | 1 | 185 | | return data; |
| | | 186 | | } |
| | | 187 | | |
| | 3 | 188 | | return data.Where(item => |
| | 3 | 189 | | { |
| | 9 | 190 | | var propertyValue = propertySelector(item); |
| | 9 | 191 | | if (propertyValue == null) |
| | 3 | 192 | | { |
| | 2 | 193 | | return false; |
| | 3 | 194 | | } |
| | 3 | 195 | | |
| | 7 | 196 | | if (exactMatch) |
| | 3 | 197 | | { |
| | 3 | 198 | | // Точное совпадение (case-insensitive) |
| | 3 | 199 | | return nonEmptyValues.Any(filterValue => |
| | 8 | 200 | | string.Equals(propertyValue, filterValue, StringComparison.OrdinalIgnoreCase)); |
| | 3 | 201 | | } |
| | 3 | 202 | | else |
| | 3 | 203 | | { |
| | 3 | 204 | | // Частичное совпадение (Contains, case-insensitive) |
| | 4 | 205 | | return nonEmptyValues.Any(filterValue => |
| | 11 | 206 | | propertyValue.IndexOf(filterValue, StringComparison.OrdinalIgnoreCase) >= 0); |
| | 3 | 207 | | } |
| | 3 | 208 | | }); |
| | | 209 | | } |
| | | 210 | | } |
| | | 211 | | } |
| | | 212 | | |