Home > Blockchain >  Auto Mapper Mapping Exception When Filtering
Auto Mapper Mapping Exception When Filtering

Time:10-19

C# beginner here, I want to only see the facilities where the manager has the same age as the current user.

For some reason when I publish my site with this code and try to view facilities, I get a very long error stating that:

***Unable to retrieve facilities from the server. Errors: "error: AutoMapper.AutoMapperMappingException: Error mapping types. Mapping types: WhereListIterator`1 -> IEnumerable`1 System.Linq.Enumerable WhereListIterator`1[ ***

Here is the function:

[HttpGet]
public async Task<IACtionResult> GetFacilities(int page, int pagesize){

var facilities = await _facilityService.GetAllfacilitiesAsync(page, pagesize);

var curr_Age = (int)HttpContext.User.GetAge();

facilities = facilities.Where(facility => facility.Manager.Age == curr_Age);

var model = _mapper.Map<IEnumerable<FacilityViewModel>>(facilities);

return OK(model);
}

I have no issue viewing all facilities when I do not filter by age so I have no idea what could be the cause of this issue?

CodePudding user response:

Issue is caused by IEnumerable being used multiple times. Possible multiple enumeration of IEnumerable

You can fix it by simply adding .ToList() after .Where(...) => facilities = facilities.Where(...).ToList();.

CodePudding user response:

Here you are trying to map IQueryable Facility into IEnumerable.

To avoid this error, try to change your code as,

facilities = facilities.Where(facility => facility.Manager.Age == curr_Age).ToList();

Read more:

  • Related