Commit e83893c6 authored by thienvo's avatar thienvo

optimize sieve

parent 7dbc4c69
...@@ -160,9 +160,10 @@ namespace Sieve.Services ...@@ -160,9 +160,10 @@ namespace Sieve.Services
} }
public Expression<Func<TEntity, bool>> GetFilterExpressionQuery<TEntity>( public Expression<Func<TEntity, bool>> GetFilterExpressionQuery<TEntity>(
TSieveModel model, TSieveModel model, ref string dynamicQuery1,
bool bCaseSensitive = false) bool bCaseSensitive = false)
{ {
//string dynamicQuery = "";
if (model?.GetFiltersParsed() == null) if (model?.GetFiltersParsed() == null)
{ {
return null; return null;
...@@ -172,6 +173,7 @@ namespace Sieve.Services ...@@ -172,6 +173,7 @@ namespace Sieve.Services
var parameterExpression = Expression.Parameter(typeof(TEntity), "e"); var parameterExpression = Expression.Parameter(typeof(TEntity), "e");
foreach (var filterTerm in model.GetFiltersParsed()) foreach (var filterTerm in model.GetFiltersParsed())
{ {
string dynamicQuery = "";
Expression innerExpression = null; Expression innerExpression = null;
foreach (var filterTermName in filterTerm.Names) foreach (var filterTermName in filterTerm.Names)
{ {
...@@ -181,6 +183,11 @@ namespace Sieve.Services ...@@ -181,6 +183,11 @@ namespace Sieve.Services
var converter = TypeDescriptor.GetConverter(property.PropertyType); var converter = TypeDescriptor.GetConverter(property.PropertyType);
dynamic propertyValue = parameterExpression; dynamic propertyValue = parameterExpression;
foreach (object attrib in property.GetCustomAttributes(true))
{
dynamicQuery = attrib.GetType().GetProperty("StringValue").GetValue(attrib, null).ToString();
// Console.WriteLine(attrib);
}
foreach (var part in fullName.Split('.')) foreach (var part in fullName.Split('.'))
{ {
propertyValue = Expression.PropertyOrField(propertyValue, part); propertyValue = Expression.PropertyOrField(propertyValue, part);
...@@ -209,6 +216,8 @@ namespace Sieve.Services ...@@ -209,6 +216,8 @@ namespace Sieve.Services
.First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0)); .First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
} }
var expression = GetExpression(filterTerm, filterValue, propertyValue, bCaseSensitive); var expression = GetExpression(filterTerm, filterValue, propertyValue, bCaseSensitive);
if (filterTerm.OperatorIsNegated) if (filterTerm.OperatorIsNegated)
...@@ -224,10 +233,23 @@ namespace Sieve.Services ...@@ -224,10 +233,23 @@ namespace Sieve.Services
{ {
innerExpression = Expression.Or(innerExpression, expression); innerExpression = Expression.Or(innerExpression, expression);
} }
dynamicQuery = GetDynamicQueryString(filterTerm, dynamicQuery.ToString(), filterValue.ToString());
} }
} }
} }
if (string.IsNullOrEmpty(dynamicQuery1))
{
dynamicQuery1 = "(" + dynamicQuery + ")";
}
else
{
dynamicQuery1 += " or " + "(" + dynamicQuery + ")";
}
if (outerExpression == null) if (outerExpression == null)
{ {
outerExpression = innerExpression; outerExpression = innerExpression;
...@@ -245,7 +267,7 @@ namespace Sieve.Services ...@@ -245,7 +267,7 @@ namespace Sieve.Services
TSieveModel model, TSieveModel model,
IQueryable<TEntity> result, IQueryable<TEntity> result,
object[] dataForCustomMethods = null, object[] dataForCustomMethods = null,
bool bCaseSensitive=false) bool bCaseSensitive = false)
{ {
if (model?.GetFiltersParsed() == null) if (model?.GetFiltersParsed() == null)
{ {
...@@ -336,7 +358,31 @@ namespace Sieve.Services ...@@ -336,7 +358,31 @@ namespace Sieve.Services
? result ? result
: result.Where(Expression.Lambda<Func<TEntity, bool>>(outerExpression, parameterExpression)); : result.Where(Expression.Lambda<Func<TEntity, bool>>(outerExpression, parameterExpression));
} }
private static string GetDynamicQueryString(TFilterTerm filterTerm, string property, string value)
{
string sResult = property;
switch (filterTerm.OperatorParsed)
{
case FilterOperator.Equals:
return sResult + "=" + value;
case FilterOperator.NotEquals:
return sResult + "!=" + value;
case FilterOperator.GreaterThan:
return sResult + ">" + value;
case FilterOperator.LessThan:
return sResult + "<" + value;
case FilterOperator.GreaterThanOrEqualTo:
return sResult + ">=" + value;
case FilterOperator.LessThanOrEqualTo:
return sResult + "<=" + value;
case FilterOperator.Contains:
return sResult + ".Contains(" + value + ")";
case FilterOperator.StartsWith:
return sResult + ".Contains(" + value + ")";
default:
return sResult + "=" + value;
}
}
private static Expression GetExpression(TFilterTerm filterTerm, dynamic filterValue, dynamic propertyValue, bool bCaseSensitive) private static Expression GetExpression(TFilterTerm filterTerm, dynamic filterValue, dynamic propertyValue, bool bCaseSensitive)
{ {
switch (filterTerm.OperatorParsed) switch (filterTerm.OperatorParsed)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment