【问题标题】:Entity Framework 6 order by using reflectionEntity Framework 6 使用反射排序
【发布时间】:2018-07-18 17:07:11
【问题描述】:

我正在尝试通过 c# 反射填充一对多关系。 我想要实现的是以下 EF 查询

_db.Entry(item).Collection(p => p.Valori).Query().OrderBy(o=>o.Ordinamento).Load();

我的要求是使用我自己的注释自动创建查询

    [EagerLoad("Ordinamento")]
    [InverseProperty("Attributo")]
    public virtual ICollection<AttributoValore> Valori { get; set; } 

直到 order by 语句一切正常

    _db.Entry(entity).Collection(propertyInfo.Name).Load();

其中 entity 是包含我要填充的集合和 propertyInfo.Name == “Valori”的对象。

我找不到正确的语法来创建这段代码的 lamda 表达式

ParameterExpression pe = Expression.Parameter(type, "x");
Expression<Func<object, K>> expr = Expression.Lambda<Func<object, K>>(Expression.Property(pe, el.ordinamento), pe);


_db.Entry(entity)
    .Collection<Object>(propertyInfo.Name)
    .Query()
    .OrderBy(expr)
    .Load();

产生异常: System.ArgumentException: 'Jerp.AttributoValore' 类型的 ParameterExpression 不能用于'System.Object' 类型的委托参数' 有人可以帮帮我吗?

【问题讨论】:

    标签: entity-framework-6 system.reflection


    【解决方案1】:

    如果有人有同样的要求。可以通过 repleflection 实现流畅的 api EF6 语法。我发现的方式有点复杂:

                        Type type = propertyInfo.PropertyType.GetGenericArguments()[0];
    
                        //_db.Entry(entity)
                        var entry = _db.Entry(entity);
    
                        //    .Collection<AttributoValore>(propertyInfo.Name)
                        MethodInfo collectionMethod = typeof(EntityEntry<T>)
                                    .GetMethod("Collection", new Type[] { typeof(String) })
                                    .MakeGenericMethod(type);
                        var query= collectionMethod.Invoke(entry, new object[] { propertyInfo.Name });
    
                        //    .Query()
                        MethodInfo queryMethod = typeof(CollectionEntry<,>)
                                    .MakeGenericType(new Type[] { typeof(T), type })
                                    .GetMethod("Query", new Type[] { });
                        var orderBy = queryMethod.Invoke(query, null);
    
                        //    .OrderByRules(new List<OrderRule> { new OrderRule(el.ordinamento, false) })
                        MethodInfo orderByMethod = typeof(MyLINQExtensions)
                                .GetMethods(BindingFlags.Static | BindingFlags.Public).FirstOrDefault()
                                .MakeGenericMethod(type);
                        var load = orderByMethod.Invoke(orderBy, new object[] { orderBy, new List<OrderRule> { new OrderRule(el.ordinamento, false) } });
    
                        //    .Load();
                        MethodInfo loadMethod = typeof(EntityFrameworkQueryableExtensions)
                                .GetMethods(BindingFlags.Static | BindingFlags.Public).Where(x => x.Name == "Load").FirstOrDefault()
                                .MakeGenericMethod(type);
                        loadMethod.Invoke(load, new object[] { load });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-31
      • 2013-05-20
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 2013-12-28
      • 2014-08-29
      • 1970-01-01
      相关资源
      最近更新 更多