Auto mapper always spliting each character when supposed to split by ','

Viewed 44

I have a string property that I am trying to split by ','

below is the code for the mapping

CreateMap<eCenterService.Domain.StudentExamPreparation, StudentExamPreparationDto>()
    .ForMember(
        des => des.DoesTheStudentPlanToTakeExam,
        m => m.MapFrom(src => !string.IsNullOrEmpty(src.DoesTheStudentPlanToTakeExam)
            ? src.DoesTheStudentPlanToTakeExam
                .Split(new char[','], System.StringSplitOptions.RemoveEmptyEntries)
            : null));

I am tring to get something like :

"doesTheStudentPlanToTakeExam": [ "SAT", "UNDECIDED" ]

but what I keep getting is:

"doesTheStudentPlanToTakeExam": [ "S", "A", "T", ",", " ", "U", "N", "D", "E", "C", "I", "D", "E", "D" ]

what am I doing wrong?

1 Answers

new char[','] due to built-in implicit conversions is actually an empty array of chars with size of 44 (new char[44], (int)',' == 44).

Change the separator from new char[','] either to just ',' or to new char[]{',', ' '} (based on your data and requirements):

CreateMap<StudentExamPreparation, StudentExamPreparationDto>()
    .ForMember(des => des.DoesTheStudentPlanToTakeExam,
        m => m.MapFrom(src =>
            !string.IsNullOrEmpty(src.DoesTheStudentPlanToTakeExam)
                ? src.DoesTheStudentPlanToTakeExam.Split(',', StringSplitOptions.RemoveEmptyEntries)
                : null));
Related