Mapping prevent null instead of string value

27 Views Asked by At

I'm trying to map the situation of equipment using this code:

CreateMap<Equipement, EquipementDto>()
    .ForMember(dest => dest.Libellesituation, opt => opt.MapFrom(src =>
        src.Lignesituationequipements == null || !src.Lignesituationequipements.Any()
            ? "Situation not assigned yet"
            : src.Lignesituationequipements
                .Where(l => l.Datefinsituation == "N/A" || DateTime.ParseExact(l.Datefinsituation, "dd-MM-yyyy", CultureInfo.InvariantCulture) > DateTime.Today)
                .OrderBy(l => DateTime.ParseExact(l.Datedebutsituation, "dd-MM-yyyy", CultureInfo.InvariantCulture))
                .Select(l => l.Code1Navigation.Libelle)
                .LastOrDefault() ?? "Situation not assigned yet"
        ));

It's working when the situation is equal to null and returns this message

Situation not assigned yet

but when the equipment has instances on the lignesituation table, it returns NULL instead of the situation name.

0

There are 0 best solutions below