0

I have two classes

 public class SourceClass
{
    public Guid Id { get; set; }
    public string Provider { get; set; }
}


public class DestinationClass
{
    public Guid Id { get; set; }
    public List<string> Provider { get; set; }
}

I've initialized my mapping by using the following code

CreateMap<SourceClass, DestinationClass>();

And then in my controller, I have :

Mapper.Map<List<DestinationClass>>(requests)

where "requests" is a List of SourceClass objects being passed in to my controller.

My question is, how can I map the Provider(of type string) in my SourceClass to Provider(of type List in my Destination class?

The provider in sourceclass will always be a single string, and provider in the destination class will always be a list of a single string.

Here is what I've tried in the mapping configurations:

CreateMap<SourceClass, DestinationClass>().ForMember(destinationMember => destinationMember.Provider,
                memberOptions => memberOptions.MapFrom(src => {
                return string.IsNullOrEmpty(src.Provider) ? [""] : src.Provider.ToList());
7
  • Can you describe in English how the string maps to a list of strings? Does it become a list containing that single string? Commented Mar 5, 2020 at 21:41
  • @Amy correct, so if the string in my source class was "Test", it should be ["Test"] in my destination class. Commented Mar 5, 2020 at 21:42
  • What issue are you running into exactly? Commented Mar 5, 2020 at 21:42
  • @TylerHundley I'm not exactly sure how to go about doing this, so I've tried using .ForMember and the ternary operator but not able to understand why it isn't working. Commented Mar 5, 2020 at 21:43
  • If the string is null or empty, its an empty list? Or is it a null list? Or does the list contain the empty string? Commented Mar 5, 2020 at 21:46

3 Answers 3

2

First off I'd recommend renaming your destination property to Providers to avoid confusion/represent that it is a collection. Then you could give this a try

CreateMap<SourceClass, DestinationClass>()
  .ForMember(destinationMember => destinationMember.Providers,
  memberOptions => memberOptions.MapFrom(src => new List<string> {src.Provider ?? ""}));

Specifically this bit

src => new List<string> {src.Provider ?? ""}

Creates a new list of type string with one value, either src.Provider, or if that is null, an empty string.

Sign up to request clarification or add additional context in comments.

Comments

0

If you cast your source member's Provider property to a List, you would get a List of char type. This code should reflect what you need.

CreateMap<SourceClass, DestinationClass>()
.ForMember(
    destinationMember => destinationMember.Provider,
    memberOptions => memberOptions.MapFrom(
        src => string.IsNullOrEmpty(src.Provider) ? new List<string>() { "" } : new List<string> { src.Provider }
    )
);

Comments

0

Replace your mapping configuration with this one:

CreateMap<SourceClass, DestinationClass>()
  .ForMember(destinationMember => destinationMember.Provider,
  memberOptions => memberOptions
    .MapFrom(src => 
    { 
      return new List<string>{ string.IsNullOrEmpty(src.Provider) ? "" : src.Provider)
    });

1 Comment

Multiple lines of code should be formatted using triple backticks or by prefixing each line with four spaces. Single backticks are for inline code.

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.