public static Payment ToDomain(this PaymentDTO payment, Payment originalPayment = null) { if (originalPayment != null && originalPayment.ID == payment.ID) { originalPayment.PaymentNo = payment.PaymentNo; originalPayment.Description = payment.Description; originalPayment.GST = payment.GST; originalPayment.Amount = payment.Amount; originalPayment.PaymentType = payment.PaymentType; originalPayment.ClientID = payment.ClientID; originalPayment.CompanyID = payment.CompanyID; originalPayment.CreateDate = payment.CreateDate; originalPayment.ChangeDate = payment.ChangeDate; originalPayment.PaymentDate = payment.PaymentDate; originalPayment.PaymentItems = payment.PaymentItems != null? payment.PaymentItems.Select(add => add.ToDomain(originalPayment.PaymentItems.FirstOrDefault(oi => oi.ID == add.ID))).ToList() : new List <PaymentItem>(); return(originalPayment); } return(new Payment() { ID = payment.ID, PaymentNo = payment.PaymentNo, Description = payment.Description, GST = payment.GST, Amount = payment.Amount, PaymentType = payment.PaymentType, ClientID = payment.ClientID, CompanyID = payment.CompanyID, Concurrency = payment.Concurrency, CreateDate = payment.CreateDate, ChangeDate = payment.ChangeDate, PaymentDate = payment.PaymentDate, PaymentItems = payment.PaymentItems.Select(ii => ii.ToDomain()).ToList() }); }