类型'Queryable'上没有泛型方法'OrderBy'与提供的类型参数兼容

Mic*_*lle 5 c# lambda expression entity-framework-4

我正在编写一些代码来修改表达式,以便包含在其中的子查询将被排序.

我在SO上找到了类似的代码:https://stackoverflow.com/a/1379693/509464 但它不适用于我也试着看这个答案,但是我无法将它应用到我的作品中代码

类型'Queryable'上没有泛型方法'OrderBy'与提供的类型参数和参数兼容.如果方法是非泛型的,则不应提供类型参数.

          MethodCallExpression orderByCallExpression = Expression.Call(
                    typeof(Queryable),
                    "OrderBy"/*Descending*/,
                    new Type[] { typeof(TSource), filterpart.OrderOverPropertyGetterValueType},
                    navigationalProperty.Body,
                    filterpart.OrderOverPropertyGetter);                            
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚哪两个类型参数或另外两个参数导致了这个错误.

  • typeof(DateTime)在这种情况下,OrderOverPropertyGetterValueType就是这样
  • TSource是一个实体类型(礼物)
  • navigationalProperty.Body包含source.Gifts.AsQueryable()带有表达式类型的{ } System.Linq.Expressions.MethodCallExpression
  • filterpart.OrderOverPropertyGetter包含{g => g.Date}表达式类型System.Linq.Expressions.Expression<System.Func<Gift,System.DateTime>>

我无能为力,因为如何诊断这四个参数是不正确的,我认为其中一个表达类型可能不正确...

Mic*_*lle 1

正如错误所示,我的类型定义是错误的。

typeof(TSource)必须如此typeof(TNav),因为我们正在订购source.Gifts