I have the following:
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);
Where:
- persons is of type
IQueryable<anonymous>
- mapper is of type
OrderMapper<anonymous>
the
OrderByWithMapper
is the following:public static IQueryable<T> OrderByWithMapper<T>(this IQueryable<T> source, OrderMapper<T> mapper) { // Method code }
OrderMapper
class is the following:public class OrderMapper { public static OrderMapper<T> For<T>(IEnumerable<T> collection) { return new OrderMapper<T>(); } } public class OrderMapper<T> { }
But I get an error when trying to create the Result:
'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>>'
I am not sure what is wrong as the OrderBy method in EntityFramework works fine with anonymous, e.g.:
public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);
What should I change in my OrderByWithMapper method to make it work with anonymous types?