From 9fb65589bb683daf6921cccce8580b1251f3f7f7 Mon Sep 17 00:00:00 2001 From: DefGh Date: Tue, 1 Aug 2023 15:21:30 +0400 Subject: [PATCH] better enum handling --- IEnumerableExtenders.sln | 6 ++++++ IEnumerableExtenders/FilterProvider.cs | 6 ++++-- IEnumerableExtenders/IEnumerableExtenders.cs | 13 ++++++++++++- IEnumerableExtenders/IEnumerableExtenders.csproj | 2 +- TestFilters/Controllers/Person.cs | 13 ++++++++++++- TestFilters/Controllers/SomeController.cs | 9 +++------ 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/IEnumerableExtenders.sln b/IEnumerableExtenders.sln index b9c9c44..9b0d54d 100644 --- a/IEnumerableExtenders.sln +++ b/IEnumerableExtenders.sln @@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestFilters", "TestFilters\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileldMapper", "FileldMapper\FileldMapper.csproj", "{2A25805E-BFDB-437C-9AC2-4EEC09A31E12}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{596D162E-EBD9-492E-AD32-84C461C160C4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -24,5 +26,9 @@ Global {2A25805E-BFDB-437C-9AC2-4EEC09A31E12}.Debug|Any CPU.Build.0 = Debug|Any CPU {2A25805E-BFDB-437C-9AC2-4EEC09A31E12}.Release|Any CPU.ActiveCfg = Release|Any CPU {2A25805E-BFDB-437C-9AC2-4EEC09A31E12}.Release|Any CPU.Build.0 = Release|Any CPU + {596D162E-EBD9-492E-AD32-84C461C160C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {596D162E-EBD9-492E-AD32-84C461C160C4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {596D162E-EBD9-492E-AD32-84C461C160C4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {596D162E-EBD9-492E-AD32-84C461C160C4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/IEnumerableExtenders/FilterProvider.cs b/IEnumerableExtenders/FilterProvider.cs index 6efbb32..87097f0 100644 --- a/IEnumerableExtenders/FilterProvider.cs +++ b/IEnumerableExtenders/FilterProvider.cs @@ -69,7 +69,7 @@ public void Add() var filter = new Filter { SourcePropertyName = sourceProperty.Name, - SourcePropertyType = sourceProperty.PropertyType, + SourcePropertyType = sourceProperty.PropertyType.IsGenericType ? sourceProperty.PropertyType.GenericTypeArguments[0] : sourceProperty.PropertyType, TargetPropertyName = targetProperty.Name, TargetPropertyType = targetProperty.PropertyType, ComparisonTypes = comparisonTypes, @@ -83,6 +83,8 @@ public void Add() foreach (var comparisonType in Enum.GetValues()) { + + var key = new FilterKey { SourceType = sourceType, @@ -90,7 +92,7 @@ public void Add() SourcePropertyName = sourceProperty.Name, TargetPropertyName = targetProperty.Name, ComparisonType = comparisonType, - SourcePropertyType = sourceProperty.PropertyType, + SourcePropertyType = sourceProperty.PropertyType.IsGenericType ? sourceProperty.PropertyType.GenericTypeArguments[0] : sourceProperty.PropertyType, TargetPropertyType = targetProperty.PropertyType }; diff --git a/IEnumerableExtenders/IEnumerableExtenders.cs b/IEnumerableExtenders/IEnumerableExtenders.cs index f62c3a8..aa6f6a7 100644 --- a/IEnumerableExtenders/IEnumerableExtenders.cs +++ b/IEnumerableExtenders/IEnumerableExtenders.cs @@ -268,6 +268,17 @@ public static List DistinctColumnValues(this IEnumerable dbSet, Li { var filter = filterProvider.GetFilter(columnName, typeof(T)); + if (filter.TargetPropertyType.IsEnum) + return Enum.GetValues(filter.TargetPropertyType).Cast().ToList(); + // same for list + if (filter.TargetPropertyType.IsGenericType && filter.TargetPropertyType.GetGenericTypeDefinition() == typeof(List<>)) + { + if (filter.TargetPropertyType.GetGenericArguments()[0].IsEnum) + return Enum.GetValues(filter.TargetPropertyType.GetGenericArguments()[0]).Cast().ToList(); + } + + + var propertyType = filter.TargetPropertyType; var query = dbSet.ApplyFilters(filters, filterProvider); @@ -291,7 +302,7 @@ public static List DistinctColumnValues(this IEnumerable dbSet, Li catch { query3 = query2; - return query3.Skip(pageSize * (page - 1)).Take(pageSize).Distinct().AsEnumerable().Select(filter.DtoConverter).ToList(); + return query3.Skip(pageSize * (page - 1)).Take(pageSize * 10).Distinct().AsEnumerable().Select(filter.DtoConverter).ToList(); } } } diff --git a/IEnumerableExtenders/IEnumerableExtenders.csproj b/IEnumerableExtenders/IEnumerableExtenders.csproj index c34712c..1a02fd9 100644 --- a/IEnumerableExtenders/IEnumerableExtenders.csproj +++ b/IEnumerableExtenders/IEnumerableExtenders.csproj @@ -5,7 +5,7 @@ enable enable PandaTech.IEnumerableFilters - 2.0.5 + 2.0.6 PandaTech This NuGet helps with filtering tables. https://github.com/PandaTechAM/be-lib-ienumerable-extenders-filters.git diff --git a/TestFilters/Controllers/Person.cs b/TestFilters/Controllers/Person.cs index 9d21e52..e853b43 100644 --- a/TestFilters/Controllers/Person.cs +++ b/TestFilters/Controllers/Person.cs @@ -34,7 +34,7 @@ public class Person public string Phone { get; set; } = null!; public List Ints { get; set; } = null!; - + public List Enums { get; set; } = null!; public double Money { get; set; } public DateTime BirthDate { get; set; } public bool IsMarried { get; set; } @@ -45,6 +45,14 @@ public class Person public List? Cats { get; set; } = null!; } +public enum MyEnum +{ + One, + Two, + Three, + Four +} + public class PersonDto { public List? Cats { get; set; } = null!; @@ -66,6 +74,9 @@ public class PersonDto public DateOnly BirthDate => DateOnly.FromDateTime(DateTime.Now).AddYears(-Age); public DateTime Now => DateTime.UtcNow; + + public List Enums { get; set; } = null!; + } public class PersonDtoMapper : IMapping diff --git a/TestFilters/Controllers/SomeController.cs b/TestFilters/Controllers/SomeController.cs index 96feb47..9645df7 100644 --- a/TestFilters/Controllers/SomeController.cs +++ b/TestFilters/Controllers/SomeController.cs @@ -132,13 +132,9 @@ public IActionResult Distinct([FromBody] GetDataRequest getDataRequest, [FromRou [HttpGet("[action]")] - public List test1() + public List test1() { - Expression> ex; - - - return _context.Persons.Where("Name.StartsWith(@0)", "D").Take(10).AsEnumerable().Select(_personDtoMapper.Map) - .ToList(); + return _context.Persons.Select(x => x.Enums).AsEnumerable().SelectMany(x => x).Distinct().Select(x => x.ToString() as object).ToList(); } [HttpGet("[action]")] @@ -188,6 +184,7 @@ public IActionResult PopulateDb() IsMarried = Random.Shared.Next(0, 3) == 0, IsWorking = Random.Shared.Next(0, 5) != 1, Ints = new List { Random.Shared.Next(0, 50), Random.Shared.Next(0, 50), Random.Shared.Next(0, 50) }, + Enums = new List() { MyEnum.One, MyEnum.Two, MyEnum.Three }, }; for (var j = 0; j < catCount; j++)