1

I have a class(Question) which contains a nested propertry called "PostedBy" which is a class called "User" and I am trying to map a datareader to IEnumerable using auto mapper and also want to populate the nested User class of each Question.

e.g.

public class Question
{
   public int  ID{ get;set; }
   public User PostedBy { get; set; }
}

public class User
{
     public string Firstname { get;set; }
     public string Lastname { get;set; }
}

I am using the following code which maps the contents of class Question ok but each nested property PostedBy ( "user" class) is always null and never gets mapped.

           Mapper.CreateMap<IDataReader, Question>().ForMember(destination => destination.PostedBy,
                              options => options.MapFrom(source => Mapper.Map<IDataReader, User>(reader)));

        //now the question information
        Mapper.CreateMap<IDataReader, IEnumerable<Question>>();
        Mapper.AssertConfigurationIsValid();

        IEnumerable<Question> returnValue = Mapper.Map<IDataReader, IEnumerable<Question>>(reader);
1
  • And it probably won't be - I'm focusing more on LINQ than IDataReader for direct-to-data support. Commented Aug 25, 2013 at 22:35

1 Answer 1

2

I've solved the problem. Here's how:

        Mapper.CreateMap<IDataReader, Question>()
            .ForMember(question => question.PostedBy,
                       o =>
                       o.MapFrom(
                           reader =>
                           new User
                               {
                                   Username = reader["Firstname"].ToString(),
                                   EmailAddress = reader["Lastname"].ToString()
                               }));
        Mapper.AssertConfigurationIsValid();

        IEnumerable<Question> mappedQuestions = Mapper.Map<IDataReader, IEnumerable<Question>>(reader);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.