I am looking to map from the list of objects to a class with booleans. I have the following structure from the source:
public partial class HomeInfoBuilding : object, System.ComponentModel.INotifyPropertyChanged
{
public HomeInfoBuildingEntry[] appliedDiscountsField;
}
public partial class HomeInfoBuildingEntry : object, System.ComponentModel.INotifyPropertyChanged
{
private string discountTypeField;
private System.Nullable<bool> discountValueField;
private bool discountValueFieldSpecified;
private string actionField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true, Order = 0)]
public string DiscountType
{
get
{
return this.discountTypeField;
}
set
{
this.discountTypeField = value;
this.RaisePropertyChanged("DiscountType");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true, Order = 1)]
public System.Nullable<bool> DiscountValue
{
get
{
return this.discountValueField;
}
set
{
this.discountValueField = value;
this.RaisePropertyChanged("DiscountValue");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool DiscountValueSpecified
{
get
{
return this.discountValueFieldSpecified;
}
set
{
this.discountValueFieldSpecified = value;
this.RaisePropertyChanged("DiscountValueSpecified");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string action
{
get
{
return this.actionField;
}
set
{
this.actionField = value;
this.RaisePropertyChanged("action");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null))
{
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
Destination:
public class AppliedDiscountInfo
{
public System.Nullable<bool> AgeDiscount { get; set; }
public System.Nullable<bool> AlarmDiscount { get; set; }
public System.Nullable<bool> BuildingTypeDiscount { get; set; }
public System.Nullable<bool> CombinedDiscount { get; set; }
public System.Nullable<bool> DriverExclBonusDiscount { get; set; }
public System.Nullable<bool> HouseholdExcellenceBonusDiscount { get; set; }
public System.Nullable<bool> MultipleProductDiscount { get; set; }
public System.Nullable<bool> MultiPetDiscount { get; set; }
public System.Nullable<bool> NoClaimDiscount { get; set; }
public System.Nullable<bool> RoadSideAssistanceLoyaltyDiscount { get; set; }
public System.Nullable<bool> SeniorCardHolderDiscount { get; set; }
}
I want the mapping to be done from the array of appliedDiscountsField to AppliedDiscountInfo as per below:
s.appliedDiscountsField.any(x => x.DiscountType == "AgeDiscount" && x.DiscountValue) => d.AgeDiscount
So basically if the source array contains a particular discount then the relevant bool in the destination should be set to true.
DiscountTypeandDiscountValueand perform mapping to destination member.DiscountTypevalues. Next time when you need to maintain the discount type, you need to modify bothAppliedDiscountInfoclass and the type converter. I am not sure that you are accepting this kind of answer or looking for some reflection way that you only need to maintainAppliedDiscountInfoclass without affecting the logic in the converter.