Ich muss eine Int-Eigenschaft aus einer Liste von Objekten zu einer List<int>
zuordnen. Hier ist, was meine Klassenstruktur wie folgt aussieht:Automapper map int Eigenschaftswerte von Liste <type> bis Liste <int>
ich eine Elternklasse haben:
public class Parent
{
...
public List<Location> Locations { get; set; }
}
Location-Klasse:
public class Location
{
public int LocationId { get; set; }
public string Name { get; set; }
}
Zielklasse für die Zuordnung:
public class Destination
{
...
public List<int> Locations { get; set; }
}
Hier ist der Code, den ich verwende, um die Zuordnung zwischenzu erreichenbis List<int>
:
CreateMap<Parent, Destination>()
.ForMember(d => d.Locations, o => o.MapFrom(s => s.Locations.Select(l => l.LocationId)))
Dies funktioniert nicht. Ich bekomme folgende Fehlermeldung:
AutoMapper.AutoMapperMappingException: Unable to create a map expression from Location.LocationId (System.Collections.Generic.IEnumerable`1[System.Int32]) to Destination.Locations (System.Collections.Generic.List`1[System.Int32])
Irgendeine Idee, was ich nicht richtig mache?
Haben Sie versucht, .ToList() am Ende Ihrer Auswahl? – JamesT
Gute Idee. Ich habe das versucht und es scheint, es behandelt zu haben. Vielen Dank! – DCATEK