1

I'm trying to project a list of entities directly from an IQueryable using AutoMapper to a dto, however it always seems to fail with an InvalidOperationException stating that The sequence contains no elements.

Entity

public class GatewaySetup {

    public static Expression<Func<GatewaySetup, ICollection<Parameter>>
        ParametersAccessor = gateway => gateway.ParametersStorage;

    protected virtual ICollection<Parameter> ParametersStorage { get; set; }

    public int Id { get; protected set; }

    public IEnumerable<Parameter> Parameters {
        get { return ParametersStorage.AsEnumerable(); } // linq2entities
    }

}

DTO

public class GatewaySetupDto {
    public int Id { get; set; }
    public List<ParameterDto> Parameters { get; set; }
}

Mapping

Mapper.CreateMap<GatewaySetup, GatewaySetupDto>()
    .ForMember(x => x.Parameters, options =>
                                    options.MapFrom(GatewaySetup.ParametersAccessor));

Api

return _repository.Project().To<GatewaySetupDto>();

What could be the cause of this? Does AutoMapper fail at the mapping between an IEnumerable<T> and a List<T>?

  • Because of this: http://stackoverflow.com/questions/15071828/convert-list-type-to-ienumerable-interface-type – Eris Jun 28 '15 at 23:17
  • Or possibly the other direction explained here: http://stackoverflow.com/questions/7617771/converting-from-ienumerable-to-list – Eris Jun 28 '15 at 23:22
  • Also, do you have a mapping for `Parameter`/`ParameterDto`? – Eris Jun 28 '15 at 23:24

0 Answers0