/// <summary> /// 获得Like的表达式 /// </summary> private static Expression GetLikeExpression(ParameterExpression entityTypeParam, string fieldName, string value) { var body = GetEntityAttrExp(entityTypeParam, fieldName, value == null); var constant = LambdaExpression.Constant(value, typeof(string)); return(Expression.Call(body, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), constant)); }
/// <summary> /// Costrói uma expressão que actua sobre o objecto. /// </summary> /// <param name="pattern">O filtro de pesquisa.</param> /// <returns>A expressão resultante.</returns> public Expression <Func <ObjectType, bool> > BuildExpressionTree(string pattern) { var patternReader = this.GetPatternReader(pattern); var stateMachine = new StateMachine <string, ELambdaExpressionWordType>( this.stateList[0], this.stateList[1]); stateMachine.RunMachine(patternReader); if (this.expressionStack.Count == 0) { // Retorna a expressão vazia var parameterExpression = LambdaExpression.Parameter( typeof(ObjectType)); var body = LambdaExpression.Constant(true); var result = LambdaExpression.Lambda <Func <ObjectType, bool> >( body, new[] { parameterExpression }); return(result); } else { var top = this.expressionStack.Pop(); var parameterExpression = LambdaExpression.Parameter( typeof(ObjectType)); var result = LambdaExpression.Lambda <Func <ObjectType, bool> >( top, new[] { parameterExpression }); return(result); } }
/// <summary> /// 根据表达式属性类型 获得 常量表达式 /// </summary> /// <param name="value"></param> /// <param name="valueType"></param> /// <returns></returns> private static ConstantExpression GetConstExp(object value, Type valueType) { if (valueType == value.GetType()) { return LambdaExpression.Constant(value, valueType); } try { //把常量转化成相对应的字段属性类型 object newObj = Convert.ChangeType(value, valueType); return LambdaExpression.Constant(newObj, valueType); } catch { return LambdaExpression.Constant(value, valueType); } }
/// <summary> /// 获得NotEquel的表达式 /// </summary> private static Expression GetNotEquelExpression(ParameterExpression entityTypeParam, string fieldName, string rightFieldName, object value) { //查询是否 join条件查询 if (!string.IsNullOrEmpty(rightFieldName)) { var fieldExp = GetEntityAttrExp(entityTypeParam, fieldName, false); var joinFieldExp = GetEntityAttrExp(entityTypeParam, rightFieldName, false); return Expression.NotEqual(fieldExp, joinFieldExp); } var body = GetEntityAttrExp(entityTypeParam, fieldName, value == null); //排除空值情况 if (value == null) { return Expression.NotEqual(body, LambdaExpression.Constant(null)); } var constant = GetConstExp(value, body.Type); return Expression.NotEqual(body, constant); }
/// <summary> /// 根据表达式属性类型 获得 常量表达式 /// </summary> /// <param name="value"></param> /// <param name="valueType"></param> /// <returns></returns> private static ConstantExpression GetConstExp(object value, Type valueType) { if (valueType == value.GetType()) { return(LambdaExpression.Constant(value, valueType)); } try { if (valueType.IsEnum) { value = Enum.Parse(valueType, value.ToString()); } //把常量转化成相对应的字段属性类型 object newObj = Convert.ChangeType(value, valueType); return(LambdaExpression.Constant(newObj, valueType)); } catch { return(LambdaExpression.Constant(value, valueType)); } }