首页 文章

在非泛型DbSet上应用动态构建的表达式

提问于
浏览
0

我有以下场景:

  • 我在编译时有一个未知的DbSet,我通过它的类型得到它:
DbSet entities = _repository.Context.Set(myType)
  • 我有一个给定类型的动态构建表达式,

Expression myFilter; //构建为 myType 的表达式,在运行时构造

如何在 entities 上应用 myFilter ,以便根据 myFilter 过滤出实体?

1 回答

  • 1

    下面是一个可能对你有帮助的代码:它最终会创建一个myu的IQueryable,它实际上表示像SELECT * FROM YourMappedTable WHERE Id = 1但是cource,而不是使用我为演示目的而构建的表达式,你可以使用你的表达 .

    class Program
    {
        static void Main(string[] args)
        {
            using (var x = new DB01Entities ())
            {
                Type myType = typeof(Angajati);
    
                var setMethod = typeof(DB01Entities).GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public).Where (a => a.Name == "Set" && a.IsGenericMethod).First ().GetGenericMethodDefinition ();
    
                var mySet = setMethod.MakeGenericMethod(myType);
    
                var realSet = mySet.Invoke(x, null);
    
                var param1 = Expression.Parameter(myType, "param1");
    
                var propertyExpresion = Expression.Property(param1, "Id");
    
                var idExpresssion = Expression.Constant(1);
    
                var body = Expression.Equal(propertyExpresion, idExpresssion);
    
                var lambda = Expression.Lambda(body, param1);
    
                var genericTypeCaster = typeof(Program).GetMethod("Caster", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).GetGenericMethodDefinition();
    
                var effectiveMethod = genericTypeCaster.MakeGenericMethod(myType);
    
                var filteredQueryable = effectiveMethod.Invoke(null, new Object[] {realSet, lambda });
            }
        }
    
        private static IQueryable<T> Caster <T> (DbSet<T> theSet, Expression whereCondition) where T : class
        {
            return theSet.Where(whereCondition as Expression<Func<T, bool>>);
        }
    }
    

    所以上面的“lambda”变量等同于你的“myFilter” . 它必须在运行时

    Expression<Func<YourType, bool>>.
    

    mySet是你的“实体”DbSet . 快乐的编码!

相关问题