实体框架(或者只是可枚举):使用反射基于子集合属性对父级进行排序

时间:2014-05-22 19:45:17

标签: c# .net linq entity-framework linq-to-sql

我几乎整天都在尝试使用反射基于CHILD集合属性对PARENT进行排序。

PS:我的子集合属性启用了延迟加载。

但是现在,我被困住了。这是我到目前为止所取得的成就:

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 }));

从db获取数据。但是当它似乎处理de order by(在内存中处理)时,我得到:

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)

1 个答案:

答案 0 :(得分:0)

根据@Servy评论,我最终使用IComparer。