匿名类型的 OrderBy
OrderBy with Anonymous Types
我有以下内容:
Context context = new Context();
var persons = context.Persons.Select(x => new { Name = x.Name });
var mapper = OrderMapper.For(persons).Add("name", x => x.Name);
var result = persons.OrderByWithMapper(mapper);
其中:
- persons 的类型是
IQueryable<anonymous>
- mapper 的类型是
OrderMapper<anonymous>
OrderByWithMapper
如下:
public static IQueryable<T> OrderByWithMapper<T>(this IQueryable<T> source, OrderMapper<T> mapper) {
// Method code
}
OrderMapper
class如下:
public class OrderMapper {
public static OrderMapper<T> For<T>(IEnumerable<T> collection) {
return new OrderMapper<T>();
}
}
public class OrderMapper<T> {
}
但是我在尝试创建结果时遇到错误:
'List<<anonymous type: string Name>>' does not contain a definition for 'OrderByWithMapper'
and the best extension method overload 'OrderByWithMapper<<anonymous type:
string Name>>(IQueryable<<anonymous type: string Name>>, OrderMapper<<anonymous type: string Name>>)'
requires a receiver of type 'IQueryable<<anonymous type: string Name>>'
我不确定有什么问题,因为 EntityFramework 中的 OrderBy 方法可以很好地处理匿名,例如:
public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);
我应该在我的 OrderByWithMapper 方法中更改什么以使其适用于匿名类型?
您的扩展方法 OrderByWithMapper<T>(this IQueryable<T> source, OrderMapper<T> mapper)
需要一个 IQueryable<T>
和一个 OrderMapper<T>
根据错误消息,您正试图在 List<<anonymous type: string Name>>
上执行它
List<T>
没有实现 IQueryable
要么更改您的扩展方法以接受其他内容(例如 IEnumerable
),要么更改您的输入以提供 IQueryable
。有关 IQueryable 用途的更多信息,请参阅 this SO post。
我有以下内容:
Context context = new Context();
var persons = context.Persons.Select(x => new { Name = x.Name });
var mapper = OrderMapper.For(persons).Add("name", x => x.Name);
var result = persons.OrderByWithMapper(mapper);
其中:
- persons 的类型是
IQueryable<anonymous>
- mapper 的类型是
OrderMapper<anonymous>
OrderByWithMapper
如下:public static IQueryable<T> OrderByWithMapper<T>(this IQueryable<T> source, OrderMapper<T> mapper) { // Method code }
OrderMapper
class如下:public class OrderMapper { public static OrderMapper<T> For<T>(IEnumerable<T> collection) { return new OrderMapper<T>(); } } public class OrderMapper<T> { }
但是我在尝试创建结果时遇到错误:
'List<<anonymous type: string Name>>' does not contain a definition for 'OrderByWithMapper'
and the best extension method overload 'OrderByWithMapper<<anonymous type:
string Name>>(IQueryable<<anonymous type: string Name>>, OrderMapper<<anonymous type: string Name>>)'
requires a receiver of type 'IQueryable<<anonymous type: string Name>>'
我不确定有什么问题,因为 EntityFramework 中的 OrderBy 方法可以很好地处理匿名,例如:
public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);
我应该在我的 OrderByWithMapper 方法中更改什么以使其适用于匿名类型?
您的扩展方法 OrderByWithMapper<T>(this IQueryable<T> source, OrderMapper<T> mapper)
需要一个 IQueryable<T>
和一个 OrderMapper<T>
根据错误消息,您正试图在 List<<anonymous type: string Name>>
List<T>
没有实现 IQueryable
要么更改您的扩展方法以接受其他内容(例如 IEnumerable
),要么更改您的输入以提供 IQueryable
。有关 IQueryable 用途的更多信息,请参阅 this SO post。