IQueryable<T>
框架中有一个实现-MSDN: EnumerableQuery<T>
IQueryable<T>.Expression
如果您可以在客户端上使用它来构建查询,则可以从属性中获取整个表达式树。
您必须对此进行测试以查看它是否适用于该表达式树序列化器。
var iQueryable = new EnumerableQuery<Model>( Enumerable.Empty<Model>() );
var query = iQueryable.Include( ... ).Where( ... ).OrderBy( ... );
var expressionTree = query.Expression;
然后,您可以序列化表达式,将其穿过电线然后反序列化。
那么问题是表达式树是基于一个EnumerableQuery<T>
.
所以你需要用IQueryable<T>
你真实的来源替换它DbContext
这有点混乱,但我已经使用ExpressionVisitor:
IQueryable FixupExpressionTree( ObjectContext ctx, Type entityType, Expression expression )
{
var tObjectContext = ctx.GetType();
var mCreateObjectSetOpen = tObjectContext.GetMethod( "CreateObjectSet", new Type[ 0 ] );
var mCreateObjectSetClosed = mCreateObjectSetOpen.MakeGenericMethod( entityType );
var objectQuery = ( ObjectQuery ) mCreateObjectSetClosed.Invoke( ctx, null );
var eFixed = new Visitor( objectQuery, entityType ).Visit( expression );
var qFixed = ( ( IQueryable ) objectQuery ).Provider.CreateQuery( eFixed );
return qFixed;
}
和它ExpressionVisitor
本身:
public class Visitor : ExpressionVisitor
{
ObjectQuery _Source = null;
Type _EntityType = null;
public Visitor( ObjectQuery source, Type entityType ) { _Source = source; _EntityType = entityType; }
protected override Expression VisitConstant( ConstantExpression node )
{
if ( !node.Type.Name.Contains( "EnumerableQuery" ) ) return base.VisitConstant( node );
var eConstantInstance = Expression.Constant( _Source );
var eConstantArgument = Expression.Constant( MergeOption.AppendOnly );
var tObjectQueryOpen = typeof( ObjectQuery<> );
var tObjectQueryClosed = tObjectQueryOpen.MakeGenericType( _EntityType );
var eMergeAsMethod = tObjectQueryClosed.GetMethod( "MergeAs", BindingFlags.Instance | BindingFlags.NonPublic );
return Expression.Call( eConstantInstance, eMergeAsMethod, eConstantArgument );
}
}
调用它是直截了当的:
Type entityType = ...
Expression expression = ...
DbContext db = ...
ObjectContext ctx = ( ( IObjectContextAdapter ) db ).ObjectContext;
IQueryable query = FixupExpressionTree( ctx, entityType, expression );