is there any way to clean this up? I have managed to solve when either media, advertiser, product, etc... are null
. Short of writing 15+ if statements, is there a way to insert values when there are values, ignore when there are no values? For instance, if estimate
!= null, but estimate["EndDate"]
== null, I still want to capture estimate["StartDate"]
...
// declare the buy type container
Dictionary<string, object> buyType = new Dictionary<string, object>();
// get the buy type objects
Dictionary<string, object> media = (Dictionary<string, object>)detail["Media"];
Dictionary<string, object> advertiser = (Dictionary<string, object>)detail["Advertiser"];
Dictionary<string, object> product = (Dictionary<string, object>)detail["Product"];
Dictionary<string, object> estimate = (Dictionary<string, object>)detail["Estimate"];
buyType.Add("CampaignId", campaignContainer["id"].ToString());
buyType.Add("PublicId", campaignContainer["PublicId"].ToString());
buyType.Add("BuyType", detail["BuyType"].ToString());
if (media != null)
{
buyType.Add("MediaCode", media["Code"].ToString());
buyType.Add("MediaName", media["Name"].ToString());
}
if (advertiser != null)
{
buyType.Add("AdvertiserCode", advertiser["Code"].ToString());
buyType.Add("AdvertiserName", advertiser["Name"].ToString());
buyType.Add("AdvertiserBusinessKey", advertiser["BusinessKey"].ToString());
}
if (product != null)
{
buyType.Add("ProductCode", product["Code"].ToString());
buyType.Add("ProductName", product["Name"].ToString());
buyType.Add("ProductBusinessKey", product["BusinessKey"].ToString());
}
if (estimate != null)
{
buyType.Add("EstimateCode", estimate["Code"].ToString());
buyType.Add("EstimateName", estimate["Name"].ToString());
buyType.Add("EstimateBusinessKey", estimate["BusinessKey"].ToString());
buyType.Add("EstimateStartDt", estimate["StartDate"].ToString());
buyType.Add("EstimateEndDt", estimate["EndDate"].ToString());
}
buyType.Add("CreatedBy", System.Reflection.Assembly.GetExecutingAssembly().FullName.ToString());
buyType.Add("CreatedDt", DateTime.Now.ToString());