【问题标题】:Dynamics CRM: null value and GetPropertyValue<T>()Dynamics CRM:空值和 GetPropertyValue<T>()
【发布时间】:2011-08-03 18:36:16
【问题描述】:

假设我有以下代码:

public class ContactDTO
{
   public string Email {get; set;}
   public decimal? ExchangeRate {get; set;}
}
......

var contacts = crm.GetEntities("contact")

var cList = new List<ContactDTO>();
foreach(contact in contacts)
{
  clist.Add(new ContactDTO
    {
      Email = contact.GetPropertyValue<string>("emailaddress1");
      ExchangeRate = contact.GetPropertyValue<decimal>("exchangerate");
    }
}

在上面的代码中,如果 Dynamics 中的汇率为空,我将取回我不想要的小数的默认值(我想知道它是否为空)。如果我要使用:

contact.GetPropertyValue<decimal?>("exchangerate")

如果它在 Dynamics 中为 null,是否应该返回一个 null?我在其他场景中尝试过这个,它总是发回值类型的默认值。如何获取 null 以确保我的 dto 对象属性为 null?

【问题讨论】:

    标签: c# dynamics-crm dynamics-crm-4 xrm


    【解决方案1】:

    我可以建议的一种方法是围绕 GetPropertyValue 方法编写一个帮助器/包装器,该方法检查返回类型并确保返回类型是否可为空(如在 contact.GetPropertyValue("exchangerate") 中),然后如果属性值也是 null 然后返回 null。 HTH。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 2015-10-25
      • 1970-01-01
      相关资源
      最近更新 更多