GetExpression 私有超載
對於一個過濾器:
這是建立查詢的位置,它接收表示式引數和過濾器。
private static Expression GetExpression<T>(ParameterExpression param, QueryFilter queryFilter)
{
// Represents accessing a field or property, so here we are accessing for example:
// the property "Name" of the entity
MemberExpression member = Expression.Property(param, queryFilter.PropertyName);
//Represents an expression that has a constant value, so here we are accessing for example:
// the values of the Property "Name".
// Also for clarity sake the GetConstant will be explained in another example.
ConstantExpression constant = GetConstant(member.Type, queryFilter.Value);
// With these two, now I can build the expression
// every operator has it one way to call, so the switch will do.
switch (queryFilter.Operator)
{
case Operator.Equals:
return Expression.Equal(member, constant);
case Operator.Contains:
return Expression.Call(member, ContainsMethod, constant);
case Operator.GreaterThan:
return Expression.GreaterThan(member, constant);
case Operator.GreaterThanOrEqual:
return Expression.GreaterThanOrEqual(member, constant);
case Operator.LessThan:
return Expression.LessThan(member, constant);
case Operator.LessThanOrEqualTo:
return Expression.LessThanOrEqual(member, constant);
case Operator.StartsWith:
return Expression.Call(member, StartsWithMethod, constant);
case Operator.EndsWith:
return Expression.Call(member, EndsWithMethod, constant);
}
return null;
}
對於兩個過濾器:
它返回 BinaryExpresion 例項而不是簡單的 Expression。
private static BinaryExpression GetExpression<T>(ParameterExpression param, QueryFilter filter1, QueryFilter filter2)
{
// Built two separated expression and join them after.
Expression result1 = GetExpression<T>(param, filter1);
Expression result2 = GetExpression<T>(param, filter2);
return Expression.AndAlso(result1, result2);
}