【问题标题】:No generic method 'OrderBy' on type 'Queryable' is compatible with the supplied type arguments'Queryable' 类型上没有通用方法 'OrderBy' 与提供的类型参数兼容
【发布时间】:2013-07-30 09:20:05
【问题描述】:

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

我发现了一段类似的代码here on SO,但它对我不起作用。我也尝试查看this 答案,但我无法将其应用于我的代码

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

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

我正在尝试找出 2 个类型参数或 2 个其他参数中的哪一个导致此错误。

  • OrderOverPropertyGetterValueType 在这只是 typeof(DateTime) 案例
  • TSource 是一个实体类型 (Gifts)
  • navigationalProperty.Body 包含 {source.Gifts.AsQueryable()} 表达式类型:System.Linq.Expressions.MethodCallExpression
  • filterpart.OrderOverPropertyGetter 包含 {g => g.Date} 表达式类型为 System.Linq.Expressions.Expression<System.Func<Gift,System.DateTime>>

我不知道如何诊断四个参数中的哪个参数不正确。我在想其中一种表达式类型可能不正确。

【问题讨论】:

    标签: c# entity-framework-4 lambda expression


    【解决方案1】:

    正如错误提示的那样,我的类型定义是错误的。

    typeof(TSource) 必须是typeof(TNav),因为我们订购的是source.Gifts

    【讨论】:

    • TNav 在哪里定义?这是什么?
    • 请提供更详细的答案。我遇到了一个场景,需要使用简单的以及通过网格的导航属性进行排序
    • TNav 是导航属性的类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2019-11-08
    • 2018-09-07
    • 2019-06-18
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多