C# 向字典添加值时,上游的值可能为空

C# 向字典添加值时,上游的值可能为空,c#,dictionary,null,C#,Dictionary,Null,有什么办法可以清理一下吗?我已经设法解决了媒体、广告商、产品等。。。是null。除了写15+if语句之外,有没有办法在有值时插入值,没有值时忽略?例如,如果估算!=null,但是estimate[“EndDate”]==null,我仍然想捕获estimate[“StartDate”] // declare the buy type container Dictionary<string, object> buyType = new Dictionary<string, obje

有什么办法可以清理一下吗?我已经设法解决了媒体、广告商、产品等。。。是
null
。除了写15+if语句之外,有没有办法在有值时插入值,没有值时忽略?例如,如果
估算
!=null,但是
estimate[“EndDate”]
==null,我仍然想捕获
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());
//声明购买类型容器
Dictionary buyType=新建字典();
//获取购买类型对象
字典媒体=(字典)详细信息[“媒体”];
字典广告商=(字典)细节[“广告商”];
字典产品=(字典)详细信息[“产品”];
字典估算=(字典)细节[“估算”];
添加(“活动id”,活动容器[“id”].ToString());
添加(“PublicId”,活动容器[“PublicId”].ToString());
buyType.Add(“buyType”,detail[“buyType”].ToString());
如果(媒体!=null)
{
buyType.Add(“MediaCode”,media[“Code”].ToString());
buyType.Add(“MediaName”,media[“Name”].ToString());
}
if(广告客户!=null)
{
添加(“广告商代码”,广告商[“代码”].ToString());
添加(“广告商名称”,广告商[“名称”].ToString());
添加(“广告商BusinessKey”,广告商[“BusinessKey”].ToString());
}
如果(产品!=null)
{
buyType.Add(“ProductCode”,product[“Code”].ToString());
buyType.Add(“ProductName”,product[“Name”].ToString());
buyType.Add(“ProductBusinessKey”,product[“BusinessKey”].ToString());
}
如果(估计值!=null)
{
添加(“EstimateCode”,estimate[“Code”].ToString());
添加(“EstimateName”,estimate[“Name”].ToString());
添加(“EstimateBuseKey”,estimate[“BusinessKey”].ToString());
添加(“EstimateStartDt”,estimate[“StartDate”].ToString());
添加(“EstimateEndDt”,estimate[“EndDate”].ToString());
}
添加(“CreatedBy”,System.Reflection.Assembly.getExecutionGassembly().FullName.ToString());
Add(“CreatedDt”,DateTime.Now.ToString());

简短的回答是“否”,您无法绕过空检查

您确实有几个选择:

  • 用try-catch(甚至每个单独的
    Add
    call)来包装整个过程。这将防止事情发生爆炸,但是如果您将整个事情包装起来,抛出行之后的任何行都不会运行,如果您执行每一行,那么您最好编写if语句

  • 如果为null,请使用null合并运算符放置defalut值:

    buyType.Add("EstimateEndDt", estimate["EndDate"] ?? String.Empty);
    
    在??支票仍然会被抛出

  • 使用三元运算符,而不是if:

     buyType.Add("EstimateEndDt", estimate["EndDate"] == null ? String.Empty : estimate["EndDate"].ToString());
    

  • 这与if语句相同,但略短一些

    你可能想看看《你有没有想过创建一个
    Tuble
    instead》,我不知道你的总体设计,但是你可能想使用带有属性的类,而不是字典。谢谢!你真棒。