VivekDev
VivekDev

Reputation: 25507

'IEnumerable<>' does not contain a definition for 'OrderBy'

Error CS1929 'IEnumerable' does not contain a definition for 'OrderBy' and the best extension method overload 'DynamicQueryableExtensions.OrderBy(IQueryable, string, params object[])' requires a receiver of type 'IQueryable'

I get this error for the following query.

var designationDtos = queryResult.Select(x =>
{
    var designationDto = ObjectMapper.Map<Designation, DesignationDto>(x.designation);
    designationDto.DepartmentName = x.department.Name;
    return designationDto;
}).OrderBy(input.Sorting).ToList();

The problem is with OrderBy. If I remove the orderby then things work fine. And input.Sorting is a string .

Upvotes: 0

Views: 3250

Answers (1)

Svyatoslav Danyliv
Svyatoslav Danyliv

Reputation: 27376

It is because IEnumerable does not have such OrderBy, but you have custom extension over IQueryable which accepts such parameters.

var designationDtos = queryResult.Select(x =>
{
    var designationDto = ObjectMapper.Map<Designation, DesignationDto>(x.designation);
    designationDto.DepartmentName = x.department.Name;
    return designationDto;
})
.AsQueryable()  
.OrderBy(input.Sorting)
.ToList();

Also consider to do not use Automapper in this query or finish mapping configuration, then query can be more effective.

query.ProjectTo<DesignationDto>(configuration)
   .OrderBy(input.Sorting)
   .ToList();

Upvotes: 5

Related Questions