Performing part of a IQueryable query and deferring the rest to Linq for Objects

前端 未结 5 1062
温柔的废话
温柔的废话 2021-02-15 15:37

I have a Linq provider that sucessfully goes and gets data from my chosen datasource, but what I would like to do now that I have my filtered resultset, is allow Linq to Objects

5条回答
  •  一个人的身影
    2021-02-15 15:56

    The kind of thing that I was after was replacing the Queryable<> constant in the expression tree with a concrete IEnumerable (or IQueryable via .AsQueryable()) result set...this is a complex topic that probably only makes any sense to Linq Provider writers who are knee deep in expression tree visitors etc.

    I found a snippet on the msdn walkthrough that does something like what I am after, this gives me a way forward...

    using System;
    using System.Linq;
    using System.Linq.Expressions;
    
    namespace LinqToTerraServerProvider
    {
        internal class ExpressionTreeModifier : ExpressionVisitor
        {
            private IQueryable queryablePlaces;
    
            internal ExpressionTreeModifier(IQueryable places)
            {
                this.queryablePlaces = places;
            }
    
            internal Expression CopyAndModify(Expression expression)
            {
                return this.Visit(expression);
            }
    
            protected override Expression VisitConstant(ConstantExpression c)
            {
                // Replace the constant QueryableTerraServerData arg with the queryable Place collection.
                if (c.Type == typeof(QueryableTerraServerData))
                    return Expression.Constant(this.queryablePlaces);
                else
                    return c;
            }
        }
    }
    

提交回复
热议问题