- 增加 表达式 DateTime - DateTime 和 DateTime - TimeSpan 的解析支持;

This commit is contained in:
28810
2019-06-21 16:48:52 +08:00
parent 5891493402
commit 4ade1c80b4
7 changed files with 239 additions and 54 deletions

View File

@ -332,6 +332,8 @@ namespace FreeSql.Internal {
static ConcurrentDictionary<Type, MethodInfo> _dicExpressionLambdaToSqlAsSelectAnyMethodInfo = new ConcurrentDictionary<Type, MethodInfo>();
internal static ConcurrentDictionary<Type, PropertyInfo> _dicNullableValueProperty = new ConcurrentDictionary<Type, PropertyInfo>();
static ConcurrentDictionary<Type, Expression> _dicFreeSqlGlobalExtensionsAsSelectExpression = new ConcurrentDictionary<Type, Expression>();
static MethodInfo MethodDateTimeSubtractDateTime = typeof(DateTime).GetMethod("Subtract", new Type[] { typeof(DateTime) });
static MethodInfo MethodDateTimeSubtractTimeSpan = typeof(DateTime).GetMethod("Subtract", new Type[] { typeof(TimeSpan) });
public string ExpressionBinary(string oper, Expression leftExp, Expression rightExp, ExpTSC tsc) {
switch (oper) {
@ -342,6 +344,12 @@ namespace FreeSql.Internal {
case "-":
if (oper == "+" && (leftExp.Type == typeof(string) || rightExp.Type == typeof(string)))
return _common.StringConcat(new[] { ExpressionLambdaToSql(leftExp, tsc), ExpressionLambdaToSql(rightExp, tsc) }, new[] { leftExp.Type, rightExp.Type });
if (oper == "-" && leftExp.Type.NullableTypeOrThis() == typeof(DateTime)) {
if (rightExp.Type.NullableTypeOrThis() == typeof(DateTime))
return ExpressionLambdaToSql(Expression.Call(leftExp, MethodDateTimeSubtractDateTime, rightExp), tsc);
if (rightExp.Type.NullableTypeOrThis() == typeof(TimeSpan))
return ExpressionLambdaToSql(Expression.Call(leftExp, MethodDateTimeSubtractTimeSpan, rightExp), tsc);
}
return $"({ExpressionLambdaToSql(leftExp, tsc)} {oper} {ExpressionLambdaToSql(rightExp, tsc)})";
}