Skip to content

Commit

Permalink
Reserve switch-statements code to consider the performance of filter …
Browse files Browse the repository at this point in the history
…function in future.
  • Loading branch information
JFTCoCo committed May 16, 2019
1 parent fa15b50 commit c9aaee6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Kendo.DynamicLinqCore.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static void Main(string[] args)
Field ="Identification",
Operator = "doesnotcontain",
Value = "4d"
}
}
},
Logic = "and"
}, null, null);
Expand Down
42 changes: 33 additions & 9 deletions src/Kendo.DynamicLinqCore/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,37 @@ public string ToExpression(Type type, IList<Filter> filters)
var typeProperties = type.GetRuntimeProperties();
var currentPropertyType = typeProperties.FirstOrDefault(f=>f.Name.Equals(Field,StringComparison.OrdinalIgnoreCase))?.PropertyType;

//switch(Operator)
//{
// case "doesnotcontain":
// if(currentPropertyType == typeof(System.String))
// return String.Format("!{0}.{1}(@{2})", Field, comparison, index);
// else
// return String.Format("({0} != null && !{0}.ToString().{1}(@{2}))", Field, comparison, index);
// case "isnull":
// case "isnotnull":
// return String.Format("{0} {1} null", Field, comparison);
// case "isempty":
// case "isnotempty":
// if(currentPropertyType == typeof(System.String))
// return String.Format("{0} {1} String.Empty", Field, comparison);
// else
// throw new NotSupportedException(String.Format("Operator {0} not support non-string type", Operator));
// case "isnullorempty":
// case "isnotnullorempty":
// if(currentPropertyType == typeof(System.String))
// return String.Format("{0}String.IsNullOrEmpty({1})", comparison, Field);
// else
// throw new NotSupportedException(String.Format("Operator {0} not support non-string type", Operator));
//}

if (Operator == "doesnotcontain")
{
if(currentPropertyType == typeof(System.String))
return String.Format("!{0}.{1}(@{2})", Field, comparison, index);
else
return String.Format("({0} != null && !{0}.ToString().{1}(@{2}))", Field, comparison, index);
}

if (comparison == "StartsWith" || comparison == "EndsWith" || comparison == "Contains")
{
if(currentPropertyType == typeof(System.String))
return String.Format("{0}.{1}(@{2})", Field, comparison, index);
else
return String.Format("({0} != null && {0}.ToString().{1}(@{2}))", Field, comparison, index);
}
}

if (Operator == "isnull" || Operator == "isnotnull")
{
Expand All @@ -145,6 +161,14 @@ public string ToExpression(Type type, IList<Filter> filters)
throw new NotSupportedException(String.Format("Operator {0} not support non-string type", Operator));
}

if (comparison == "StartsWith" || comparison == "EndsWith" || comparison == "Contains")
{
if(currentPropertyType == typeof(System.String))
return String.Format("{0}.{1}(@{2})", Field, comparison, index);
else
return String.Format("({0} != null && {0}.ToString().{1}(@{2}))", Field, comparison, index);
}

return String.Format("{0} {1} @{2}", Field, comparison, index);
}
}
Expand Down

0 comments on commit c9aaee6

Please sign in to comment.