Skip to content

Commit

Permalink
🔨 avoid duplicate property search conditions when search value is num…
Browse files Browse the repository at this point in the history
…ber.
  • Loading branch information
NazarovMikhail committed Jan 15, 2024
1 parent b555e70 commit 912e747
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/Monq.Core.Paging/Extensions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ public static IQueryable<TSource> OrderByProperty<TSource>(this IQueryable<TSour
searching ??= new Searching();

var props = typeof(TSource).GetPublicProperties(searching.Depth, searching.InSearch).ToList();
var stringProperties = props.Where(p => _supportSearchByStringPropertyTypes
var searchByIntProperties = long.TryParse(search, out _)
? props.Where(p => _supportSearchByIntNumberPropertyTypes.Contains(p.Property.PropertyType)).ToList()
: new List<(string, PropertyInfo)>();
var searchByStringProperties = props.Where(p => _supportSearchByStringPropertyTypes
.Contains(p.Property.PropertyType))
.Except(searchByIntProperties)
.ToList();

if (!props.Any())
Expand All @@ -84,23 +88,17 @@ public static IQueryable<TSource> OrderByProperty<TSource>(this IQueryable<TSour
var value = Expression.Constant(search.ToLower(), typeof(string));
var expList = new List<BinaryExpression>();

foreach (var (fullName, prop) in stringProperties)
foreach (var (fullName, prop) in searchByStringProperties)
{
BinaryExpression extAnd = prop.PropertyType == typeof(string)
? FormExpressionForStringProperty(parameter, value, fullName)
: FormExpressionForValueProperty(parameter, value, fullName);
expList.Add(extAnd);
}

if (long.TryParse(search, out _))
foreach (var (fullName, _) in searchByIntProperties)
{
var searchByIntProperties = props.Where(p => _supportSearchByIntNumberPropertyTypes.Contains(p.Property.PropertyType))
.ToList();
foreach (var (fullName, _) in searchByIntProperties)
{
BinaryExpression extAnd = FormExpressionForValueProperty(parameter, value, fullName);
expList.Add(extAnd);
}
BinaryExpression extAnd = FormExpressionForValueProperty(parameter, value, fullName);
expList.Add(extAnd);
}

if (expList.Count == 0)
Expand Down

0 comments on commit 912e747

Please sign in to comment.