Commit e83893c6 authored by thienvo's avatar thienvo

optimize sieve

parent 7dbc4c69
......@@ -160,9 +160,10 @@ namespace Sieve.Services
}
public Expression<Func<TEntity, bool>> GetFilterExpressionQuery<TEntity>(
TSieveModel model,
TSieveModel model, ref string dynamicQuery1,
bool bCaseSensitive = false)
{
//string dynamicQuery = "";
if (model?.GetFiltersParsed() == null)
{
return null;
......@@ -172,6 +173,7 @@ namespace Sieve.Services
var parameterExpression = Expression.Parameter(typeof(TEntity), "e");
foreach (var filterTerm in model.GetFiltersParsed())
{
string dynamicQuery = "";
Expression innerExpression = null;
foreach (var filterTermName in filterTerm.Names)
{
......@@ -181,6 +183,11 @@ namespace Sieve.Services
var converter = TypeDescriptor.GetConverter(property.PropertyType);
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('.'))
{
propertyValue = Expression.PropertyOrField(propertyValue, part);
......@@ -209,6 +216,8 @@ namespace Sieve.Services
.First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
}
var expression = GetExpression(filterTerm, filterValue, propertyValue, bCaseSensitive);
if (filterTerm.OperatorIsNegated)
......@@ -224,10 +233,23 @@ namespace Sieve.Services
{
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)
{
outerExpression = innerExpression;
......@@ -245,7 +267,7 @@ namespace Sieve.Services
TSieveModel model,
IQueryable<TEntity> result,
object[] dataForCustomMethods = null,
bool bCaseSensitive=false)
bool bCaseSensitive = false)
{
if (model?.GetFiltersParsed() == null)
{
......@@ -336,7 +358,31 @@ namespace Sieve.Services
? result
: 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)
{
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