public override DbExpression Visit(DbConvertExpression exp) { DbExpression stripedExp = DbExpressionHelper.StripInvalidConvert(exp); if (stripedExp.NodeType != DbExpressionType.Convert) { stripedExp.Accept(this); return(exp); } exp = (DbConvertExpression)stripedExp; string dbTypeString; if (TryGetCastTargetDbTypeString(exp.Operand.Type, exp.Type, out dbTypeString, false)) { this.BuildCastState(exp.Operand, dbTypeString); } else { exp.Operand.Accept(this); } return(exp); }
/// <summary> /// 尝试将 exp 转换成 DbParameterExpression。 /// </summary> /// <param name="exp"></param> /// <returns></returns> public static DbExpression ParseDbExpression(this DbExpression exp) { DbExpression stripedExp = DbExpressionHelper.StripInvalidConvert(exp); DbExpression tempExp = stripedExp; List <DbConvertExpression> cList = null; while (tempExp.NodeType == DbExpressionType.Convert) { if (cList == null) { cList = new List <DbConvertExpression>(); } DbConvertExpression c = (DbConvertExpression)tempExp; cList.Add(c); tempExp = c.Operand; } if (tempExp.NodeType == DbExpressionType.Constant || tempExp.NodeType == DbExpressionType.Parameter) { return(stripedExp); } if (tempExp.NodeType == DbExpressionType.MemberAccess) { DbMemberExpression dbMemberExp = (DbMemberExpression)tempExp; if (ExistDateTime_NowOrDateTime_UtcNow(dbMemberExp)) { return(stripedExp); } DbParameterExpression val; if (DbExpressionExtensions.TryParseToParameterExpression(dbMemberExp, out val)) { if (cList != null) { if (val.Value == DBNull.Value)//如果是 null,则不需要 Convert 了,在数据库里没意义 { return(val); } DbConvertExpression c = null; for (int i = cList.Count - 1; i > -1; i--) { DbConvertExpression item = cList[i]; c = new DbConvertExpression(item.Type, val); } return(c); } return(val); } } return(stripedExp); }