I have the following code.
All of the fields map nicely from the source to the destination object.
However, there is one field in the destination object that I will need to compute.
For example:
DestinationObj.Status
can be set depending on certain fields from the SourceObj
.
If I were to write code, it would look similar to:
foreach (var rec in listData)
{
string status;
if (!String.IsNullOrEmpty(rec.Field1))
{
status = "Stage 1";
}
if (!String.IsNullOrEmpty(rec.Field2))
{
status = "Stage 2";
}
if (!String.IsNullOrEmpty(rec.Field3))
{
status = "Stage 3";
}
}
Can I do something similar in AutoMapper?
var config = new MapperConfiguration(c =>
{
c.CreateMap<SourceObj, DestinationObj>()
.ForMember(x => x.Status, m => m.MapFrom([Not sure at this point]));
});
EDIT:
List<destinationObj> destinObj = new List<destinationObj>();
foreach (var rec in listSourceObject)
{
destinationObj do = new destinationObj();
// Manually map all of the fields...
string status;
if (!String.IsNullOrEmpty(rec.Field1))
{
do.status = "Stage 1";
}
if (!String.IsNullOrEmpty(rec.Field2))
{
do.status = "Stage 2";
}
if (!String.IsNullOrEmpty(rec.Field3))
{
do.status = "Stage 3";
}
destinObj.Add(do);
}