c# - 使用类型转换包装 Expression<Func<T,bool>>

标签 c# generics lambda iqueryable

我的存储库返回从公共(public)基类派生的实体

class BaseClass
{
    public int Id { get; set; }
}

class MyClass : BaseClass
{
    public string Name { get; set; }
}

class MyOtherClass : BaseClass
{
    ...
}

在这样的函数中:

IQueryable<T> GetEntities<T>() where T : BaseClass

我添加了一种方法,使用 Expression<Func<T,bool>> 将特定实体的附加过滤器注册为 lambda。像这样:

RegisterFilter<MyClass>(t => t.Name == "Test" );

每当 GetEntities 时都会应用该规则被调用 MyClass在类型参数中。

问题

如何在运行时动态创建一个表达式,将类型转换包装在过滤器周围?

在我的具体案例中GetEntities被调用 IQueryable<MyClass>使用BaseClass作为类型参数和事件,我知道 MyClass 的过滤器需要应用我没有找到方法:

IQueryable<BaseClass> src =
    (new List<MyClass>
    {
        new MyClass { Id = 1, Name = "asdf" },
        new MyClass { Id = 2, Name = "Test" }
    })
    .AsQueryable();

Expression<Func<MyClass, bool>> filter = o => o.Name == "Test";

// does not work (of course)
src.Where(filter);

尝试失败

显然我可以在调用 Where 之前将集合强制转换回来但我在运行时执行此操作的尝试没有成功:

// HACK: don't look
var genericCast = typeof(Queryable).GetMethod("Cast").MakeGenericMethod(entityType);
var genericWhere = typeof(Queryable).GetMethods().Single(mi => mi.Name == "Where" && mi.GetParameters()[1].ParameterType.GenericTypeArguments[0].Name == "Func`2");
q = (IQueryable<T>)genericCast.Invoke(q, new object[] { genericWhere.Invoke(q, new object[] { filterExp }) });

System.InvalidOperationException: 'Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.'

因为这也非常丑陋,所以我尝试将我的过滤器包裹在类型转换中,如下所示:

LambdaExpression filterExp = (LambdaExpression)filter;
var call = filterExp.Compile();

Expression<Func<T, bool>> wrap = o => (bool)call.DynamicInvoke(Convert.ChangeType(o, entityType));

这可以工作,但会阻止将过滤器生成到存储表达式中,因此它将在内存中完成,这也是不可取的。

我觉得这个问题的解决方案应该很简单,但我似乎无法正确解决,因此非常感谢任何帮助。

最佳答案

你可以:

// Using SimpleExpressionReplacer from https://stackoverflow.com/a/29467969/613130
public static Expression<Func<BaseClass, bool>> Filter<TDerived>(Expression<Func<TDerived, bool>> test)
    where TDerived : BaseClass
{
    // x => 
    var par = Expression.Parameter(typeof(BaseClass), "x");

    // x is TDerived
    var istest = Expression.TypeIs(par, typeof(TDerived));

    // (TDerived)x
    var convert = Expression.Convert(par, typeof(TDerived));

    // If test is p => p.something == xxx, replace all the p with ((TDerived)x)
    var test2 = new SimpleExpressionReplacer(test.Parameters[0], convert).Visit(test.Body);

    // x is TDerived && test (modified to ((TDerived)x) instead of p)
    var and = Expression.AndAlso(istest, test2);

    // x => x is TDerived && test (modified to ((TDerived)x) instead of p)
    var test3 = Expression.Lambda<Func<BaseClass, bool>>(and, par);

    return test3;
}

请注意,我正在使用 SimpleExpressionReplacer我在另一个答案里写过。

给出如下测试:

var exp = Filter<MyClass>(p => p.Name == "Test");

结果表达式是:

x => x is MyClass && ((MyClass)x).Name == "Test"

关于c# - 使用类型转换包装 Expression<Func<T,bool>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51421075/

相关文章:

c# - 配置 DataGridView 的 'AutoGenerateColumns' 属性

c# - Selenium:如何拦截请求

Java Collections.sort - 帮助我删除未经检查的警告

Java lambda表达式冗余调用外部方法——如何重写?

c++ - 在 std::function 和 lambda 中使用自动说明符

c# - 将属性绑定(bind)到 itemssource 集合的属性

c# - 具有语法高亮显示的文本框/富文本框? [C#]

c# - 使用接口(interface)和显式实现的函数重载

scala - 为什么 scala 编译器说这种类型用在非特化位置?

amazon-web-services - AWS Lambda cloudwatch 日志抛出 "Failed to load events"错误