I want to know how I can achieve the following mapping via Automapper.
public class MyTestClass
{
public int BrandId;
public decimal? Amount;
public string Currency;
public List<MyMappingClass> MappingClassList;
}
public class MyMappingClass
{
public string RefNumber;
public int TransRef;
}
public class MyDestDBClass
{
public int BrandId;
public decimal? Amount;
public string Currency;
public string RefNumber;
public int TransRef;
}
I have written the following logic were I am looking to populate Amount & currency for 1st Item. The Second Item should have null Amount & currency.
C# Code:
var inputObj = new MyMappingClass()
{
BrandId = 1,
Amount = 10.00,
Currency = "USD",
MappingClassList = new List<MyMappingClass>()
{
new MyMappingClass(){ RefNumber = "S123", TransRef = "4567" },
new MyMappingClass(){ RefNumber = "S1234", TransRef = "45679" },
}
};
var isFirstItem = true;
var dbObjList = new List<MyDestDBClass>();
inputObj.MappingClassList.ForEach(x=>
{
var outObj = new MyDestDBClass()
{
BrandId = inputObj.BrandId ,
RefNumber = x.RefNumber,
TransRef = x.TransRef,
Amount = isFirstItem ? inputObj.Amount : null,
Currency = isFirstItem ? inputObj.Currency: null,
};
dbObjList.Add(outObj);
isFirstItem = false;
});
I tried Automappers ConstructProjectionUsing Where I was able to loop through and create a list but unable to apply the first item check in the mapping config.
I expect the following output:
dbObjList = List<MyDestDBClass>()
{
new MyDestDBClass() {BrandId = 1 ,RefNumber = S123,TransRef = 4567,Amount = 10.00 , Currency = "USD" },
new MyDestDBClass() {BrandId = 1 ,RefNumber = S123,TransRef = 4567,Amount = null , Currency = null }
}