Dynamics CRM:空值和 GetPropertyValue()
假设我有下面的代码:
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?
Let's say I have the code below:
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");
}
}
In the code above if exchange rate is null in Dynamics I'm going to get back the default value for a decimal which is not what I want (I want to know if it is null). If I were to use:
contact.GetPropertyValue<decimal?>("exchangerate")
Should that bring back a null if it's null in Dynamics? I've tried this in other scenarios and it always sends back the default value for the value type. How can I get null back so that i can make sure that my dto object property is null?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议的一种方法是围绕 GetPropertyValue 方法编写一个帮助器/包装器,该方法检查返回类型并确保返回类型是否可为空(如 contact.GetPropertyValue("exchangerate") 中所示),然后属性值是否也为 null然后返回 null。 HTH。 :)
One way I can suggest is to write a helper/wrapper around the GetPropertyValue method that checks the return type and makes sure if the return type is nullable ( as in contact.GetPropertyValue("exchangerate")) then if the property value is also null then returns null. HTH. :)