I have the following dto:
public class SingleForm
{
// other props left out for brevity
public List<Filter> Filters { get; set; }
}
I then try mapping it with AutoMapper like so:
CreateMap<Form, SingleForm>()
.ForMember(dest => dest.Filters,
opts => opts.MapFrom(src =>
Mapper.Map<List<Filter>>(src.Questions)));
CreateMap<FormQuestion, Filter>()
.ForMember(dest => dest.Header,
opts => opts.MapFrom(src => src.Question.QuestionText));
I then use ProjectTo:
var query = this.context.Forms
.Where(e => e.Id == message.FormId)
.ProjectTo<SingleForm>()
.FirstOrDefault();
However, my filters collection is empty when I execute the query.
When I try to manually map the collection using LINQ, like below, it works correctly, so I'm wondering if I am doing something wrong?
var query = this.context.Forms
.Where(e => e.Id == message.FormId)
.Select(e => new SingleForm
{
Id = e.Id,
Filters = e.Questions.Select(q =>
new Filter {
Header = q.Question.QuestionText
}).ToList()
})
.FirstOrDefault();