Skip to content

Commit 2dd722e

Browse files
committed
- 优化 WhereDynamicFilter 忽略 like '%%';
1 parent 6cba1de commit 2dd722e

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

Examples/base_entity/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static void Main(string[] args)
174174
{
175175
""Field"" : ""title"",
176176
""Operator"" : ""contains"",
177-
""Value"" : ""product-1111"",
177+
""Value"" : """",
178178
},
179179
{
180180
""Field"" : ""title"",

FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ public TSelect WhereDynamicFilter(DynamicFilterInfo filter)
514514
{
515515
if (filter == null) return this as TSelect;
516516
var sb = new StringBuilder();
517+
if (IsIgnoreFilter(filter)) filter.Field = "";
517518
ParseFilter(DynamicFilterLogic.And, filter, true);
518519
this.Where(sb.ToString());
519520
sb.Clear();
@@ -622,12 +623,17 @@ string[] getFiListValue()
622623
}
623624
if (fi.Filters?.Any() == true)
624625
{
625-
if (string.IsNullOrEmpty(fi.Field) == false)
626-
sb.Append(" AND ");
627-
if (fi.Logic == DynamicFilterLogic.Or) sb.Append("(");
628-
for (var x = 0; x < fi.Filters.Count; x++)
629-
ParseFilter(fi.Logic, fi.Filters[x], x == fi.Filters.Count - 1);
630-
if (fi.Logic == DynamicFilterLogic.Or) sb.Append(")");
626+
fi.Filters = fi.Filters.Where(a => IsIgnoreFilter(a) == false).ToList(); //忽略 like '%%'
627+
628+
if (fi.Filters.Any())
629+
{
630+
if (string.IsNullOrEmpty(fi.Field) == false)
631+
sb.Append(" AND ");
632+
if (fi.Logic == DynamicFilterLogic.Or) sb.Append("(");
633+
for (var x = 0; x < fi.Filters.Count; x++)
634+
ParseFilter(fi.Logic, fi.Filters[x], x == fi.Filters.Count - 1);
635+
if (fi.Logic == DynamicFilterLogic.Or) sb.Append(")");
636+
}
631637
}
632638

633639
if (isend == false)
@@ -642,6 +648,13 @@ string[] getFiListValue()
642648
}
643649
}
644650
}
651+
652+
bool IsIgnoreFilter(DynamicFilterInfo testFilter)
653+
{
654+
return string.IsNullOrEmpty(testFilter.Field) == false &&
655+
new[] { DynamicFilterOperator.Contains, DynamicFilterOperator.StartsWith, DynamicFilterOperator.EndsWith }.Contains(testFilter.Operator) &&
656+
string.IsNullOrEmpty(testFilter.Value?.ToString());
657+
}
645658
}
646659

647660
public TSelect DisableGlobalFilter(params string[] name)

0 commit comments

Comments
 (0)