修复method call expression 的bug

This commit is contained in:
xuejiaming 2022-09-14 12:30:10 +08:00
parent 38b53e883a
commit 9b788bd77b
1 changed files with 39 additions and 13 deletions

View File

@ -86,6 +86,18 @@ namespace ShardingCore.Core.Internal.Visitors
Expression.Property(ConstantExpression.Constant(tempVariable), nameof(TempVariable<object>.Queryable)); Expression.Property(ConstantExpression.Constant(tempVariable), nameof(TempVariable<object>.Queryable));
return queryableMemberReplaceExpression; return queryableMemberReplaceExpression;
} }
private MethodCallExpression ReplaceMethodCallExpression(IQueryable queryable)
{
var dbContextReplaceQueryableVisitor = new DbContextReplaceQueryableVisitor(_dbContext);
var newExpression = dbContextReplaceQueryableVisitor.Visit(queryable.Expression);
var newQueryable = dbContextReplaceQueryableVisitor.Source.Provider.CreateQuery(newExpression);
var tempVariableGenericType = typeof(TempVariable<>).GetGenericType0(queryable.ElementType);
var tempVariable = Activator.CreateInstance(tempVariableGenericType, newQueryable);
// MemberExpression queryableMemberReplaceExpression =
// Expression.Property(, nameof(TempVariable<object>.Queryable));
return Expression.Call(ConstantExpression.Constant(tempVariable),tempVariableGenericType.GetMethod(nameof(TempVariable<object>.GetQueryable)),new Expression[0]);
}
private MemberExpression ReplaceMemberExpression(DbContext dbContext) private MemberExpression ReplaceMemberExpression(DbContext dbContext)
{ {
@ -108,26 +120,35 @@ namespace ShardingCore.Core.Internal.Visitors
#endif #endif
if (notRoot) if (notRoot)
{ {
var entityType = node.Method.ReturnType.GenericTypeArguments[0]; var objQueryable = Expression.Lambda(node).Compile().DynamicInvoke();
if (objQueryable != null && objQueryable is IQueryable queryable)
var whereCallExpression = ReplaceMethodCallExpression(node, entityType); {
return whereCallExpression; return ReplaceMethodCallExpression(queryable);
// var whereCallExpression = ReplaceMethodCallExpression(replaceMemberExpression);
// return base.VisitMethodCall(whereCallExpression);;
// Console.WriteLine("1");
}
} }
} }
return base.VisitMethodCall(node); return base.VisitMethodCall(node);
} }
private MethodCallExpression ReplaceMethodCallExpression(MethodCallExpression methodCallExpression, // private MethodCallExpression ReplaceMethodCallExpression(MemberExpression memberExpression)
Type entityType) // {
// var lambdaExpression = GetType().GetMethod(nameof(WhereTrueExpression)).MakeGenericMethod(new Type[] { queryable.ElementType }).Invoke(this,new object[]{});
// MethodCallExpression whereCallExpression = Expression.Call(
// typeof(Queryable),
// nameof(Queryable.Where),
// new Type[] { queryable.ElementType },
// queryable.Expression, (LambdaExpression)lambdaExpression
// );
// return whereCallExpression;
// }
public Expression<Func<T, bool>> WhereTrueExpression<T>()
{ {
MethodCallExpression whereCallExpression = Expression.Call( return t => true;
typeof(IShardingQueryableExtension),
nameof(IShardingQueryableExtension.ReplaceDbContextQueryableWithType),
new Type[] { entityType },
methodCallExpression, Expression.Constant(_dbContext)
);
return whereCallExpression;
} }
@ -139,6 +160,11 @@ namespace ShardingCore.Core.Internal.Visitors
{ {
Queryable = queryable; Queryable = queryable;
} }
public IQueryable<T1> GetQueryable()
{
return Queryable;
}
} }
internal sealed class TempDbVariable<T1> internal sealed class TempDbVariable<T1>