1

I have one proble for creating mapping from array to object type. so anybody have a answer for this then please help me.

view model (Source class) :

public class HealthView : IView
{
    public Guid Id { get; set; }
    public string Type { get; set; }
    public string Value { get; set; }

    [JsonIgnore]
    public DateTime? HealthCheckDateTime { get; set; }
    public string HealthCheckDateTimeString { get { return HealthCheckDateTime.GetValueOrDefault().ToString(CultureInfo.InvariantCulture); } }
}

converted in this (Destination Class):

    public class HealthResponse : WebApiResonseBase
{
    public HealthResponse()
    {
        Value = new HealthLine[0];
    }

    public HealthLine[] Value { get; set; }

    public class HealthLine
    {
        public Guid Id { get; set; }
        public string Type { get; set; }
        public string Value { get; set; }
        public DateTime? HealthCheckDateTime { get; set; }
        public string HealthCheckDateTimeString { get; set; }
    }
}

mapping :

 CreateMap<HealthView[], HealthResponse>()
            .ForMember(x => x.RedirectRequired, o => o.Ignore())
            .ForMember(x => x.Uri, o => o.Ignore());

This is my whole procedure, i try to different way but i got errros.

4
  • What are you trying to achieve? Commented Jan 27, 2014 at 10:32
  • I have solving this problem. but some problem arise my side. so i will give answer later. Commented Jan 27, 2014 at 10:43
  • i am trying array to object member mapping using automapper.. Commented Jan 27, 2014 at 10:45
  • Please specify the errors. Commented Jan 27, 2014 at 10:46

2 Answers 2

3

I have solved this problem with this code.

Mapping :

 CreateMap<HealthView, HealthResponse.HealthLine>();

In Controller :

 var response = new HealthResponse
        {
            Value = healthView.Select(Mapper.Map<HealthView, HealthResponse.HealthLine>).ToArray()
        };
Sign up to request clarification or add additional context in comments.

Comments

2

I take it that you want to map a HealthView to a HealthLine so try this:

CreateMap<HealthView, HealthView>();

var response = new HealthResponse();
var views = an array of HealthView objects from somewhere.

response.Value = Mapper.Map<IEnumerable<HealthView>,IEnumerable<HealthLine>>(views);

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.