Можно ли сопоставить ICollection с единым объектом BonusDto?
Я использую ICollection внутри класса Customer, чтобы получить все строки и из строк, я вычисляю поля Retained & PreviouslyPaid в частичном классе Bonus, который я хочу сопоставить с BonusDto.
public class BonusDto
{
public virtual long CustomerId { get; set; }
public virtual decimal Retained { get; set; }
public virtual decimal PreviouslyPaid { get; set; }
}
public class Customer
{
public virtual string Firstname { get; set; }
public virtual ICollection<Bonus> Bonuses { get; set; }
}
public class CustomerDto
{
public virtual string Firstname { get; set; }
public virtual BonusDto Bonus { get; set; }
}
public partial class Bonus
{
private decimal _previouslyPaid;
public decimal PreviouslyPaid
{
get
{
if (UsedDate.HasValue)
{
_previouslyPaid += Amount;
}
return _previouslyPaid;
}
}
private decimal _retained;
public decimal Retained
{
get
{
if (!UsedDate.HasValue && DueDate > DateTime.UtcNow)
{
_retained += Amount;
}
return _retained;
}
}
}
Вы должны определить отображение Bonuses
в Bonus
используя ForMember
и ResolveUsing
. Передайте свою собственную логику отображения от ICollection<Bonus>
до одного BonusDto
:
static BonusDto YourLogic(Customer customer)
{
return new BonusDto()
{
Retained = customer.Bonuses.Select(b => b.Retained).Sum()
};
}
static void Main(string[] args)
{
AutoMapper.Mapper.CreateMap<Customer, CustomerDto>()
.ForMember(dto => dto.Bonus, opt => opt.ResolveUsing(YourLogic));
var customerDto = AutoMapper.Mapper.Map<CustomerDto>(customer);
}
Bonus
внутри коллекции в одноBonusDto
?