简体   繁体   English

实体框架(或仅可枚举):使用反射根据子级集合属性对父级进行排序

[英]Entity Framework (Or Just Enumerable): Sort parent based on child collection property using reflection

I have spent almost my whole day trying to sort PARENT based on CHILD collection property using reflection. 我几乎整天都在尝试使用反射根据CHILD集合属性对PARENT进行排序。

PS: My child collection property has lazy loading enabled. PS:我的孩子收藏属性启用了延迟加载。

But now, I'm stuck. 但是现在,我被困住了。 Here is what I have achieved till now: 这是我到目前为止所取得的成就:

var query = myIQueryAbleVariable;

var collectionName = "MyCollectionName";
var collectionType = typeof (TEntity).GetProperty(collectionName).PropertyType;
var collectionItemType = collectionType.GetGenericArguments()[0];

var propertyName = "CollectionPropertyName";
var propertyInfo = collectionItemType.GetProperty(propertyName);

// Create a parameter to use for both of the expression bodies.
var parameter = Expression.Parameter(collectionItemType, "x");
Expression columnExpr = Expression.Property(parameter, propertyInfo);
var lambda = Expression.Lambda(columnExpr, parameter);

var selectMethod = typeof(Enumerable)
   .GetMethods(BindingFlags.Static | BindingFlags.Public)
   .FirstOrDefault(
      mi =>
         mi.Name == "Select" &&
         mi.GetParameters()[1].ParameterType.GetGenericArguments().Count() == 2);

// we need to specialize it 
selectMethod = selectMethod.MakeGenericMethod(collectionItemType, lambda.GetType());

query = query.OrderBy(p =>
   selectMethod.Invoke(null,
      new[] { p.GetType().GetProperty(collectionName).GetValue(p), lambda }));

It fetches data from db. 它从数据库获取数据。 But when it seems to process de order by (which is processed in memory), I get: 但是,当似乎按顺序处理(在内存中处理)时,我得到:

System.ArgumentException: Object of type 'System.Linq.Expressions.Expression`1[System.Func`2[Measure,System.Int32]]' cannot be converted to type 'System.Func`2[Measure,System.Linq.Expressions.Expression`1[System.Func`2[Measure,System.Int32]]]'.
   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at lambda_method(Closure , Measure )
   at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)
   at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)

基于@Servy注释,我最终使用了IComparer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM