2

我正在尝试为 IQueryable 创建一个扩展方法,该方法通过对象的任意属性对其进行排序。

public static class IQueryableExtender

{

public static IQueryable<TSource> Sort<TSource>(this IQueryable<TSource> query, string orderProperty, string sortDirection = "asc")      
{

    var elementType = query.ElementType;

    var propertyInfo = elementType.GetProperty(orderProperty);
    if (propertyInfo == null)
    {
         throw new ArgumentException(string.Format("{0} is not a property on {1}", orderProperty, elementType.Name));
    }


    switch (sortDirection.ToLower())
    {
       case "asc":
       case "ascending":
           return query.OrderBy(x => propertyInfo.GetValue(x, null));
           break;
       case "desc":
       case "descending":
           return query.OrderByDescending(x => propertyInfo.GetValue(x, null));
           break;
    }

    return query;
}
}

调用该方法时出现此错误。我想这与 IQueryable 尚未执行和检索任何对象有关。

LINQ to Entities 无法识别该方法System.Object GetValue(System.Object, System.Object[]),并且该方法无法转换为存储表达式。

我可以通过在 IQueryable 上执行 ToList 来解决它,但是然后我在扩展方法中检索数据,这不是我想要的。

这可以解决吗?

4

1 回答 1

2

当您对 IQueryable<> 执行 LINQ 操作时,LINQ to Entities 会尝试在数据库中运行您的查询。在您的case "asc"中,您执行 query.OrderBy,LINQ to Entities 将其解释为“将其转换为 SQL”,并且由于您使用反射调用而失败,它不知道如何转换为 SQL。

你可以做 query.AsEnumerable().OrderBy(...)。这样做的一个效果是,随着 OrderBy 操作开始运行,查询的其余部分将按顺序执行以提供数据。

除了使用这些反射技巧,您可以简单地通过使用 OrderBy 和 OrderByDescending 方法来解决问题,这些方法旨在使用委托来提取排序值。( items.OrderBy(item => item.Property))。你缺少的是在同一个方法中指定升序或降序的能力,但我会简单地做一对方法,比如:

public static IOrderedQueryable<TSource> OrderByAscDesc<TSource, TKey>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, TKey>> keySelector, bool isAsc
) {
    return (isAsc ? source.OrderBy(keySelector) : source.OrderByDescending(keySelector);
}

public static IOrderedQueryable<TSource> OrderByAscDesc<TSource, TKey>(
    this IQueryable<TSource> source,
    Func<TSource, TKey> keySelector, bool ascDesc
) {
    return (isDesc ? source.OrderBy(keySelector) : source.OrderByDescending(keySelector);
}
于 2011-05-24T07:59:38.477 回答