开发者

Dynamics CRM: null value and GetPropertyValue<T>()

开发者 https://www.devze.com 2023-03-25 12:10 出处:网络
Let\'s say I have the code below: public class ContactDTO { public string Email {get; set;} public decimal? ExchangeRate {g开发者_开发知识库et; set;}

Let's say I have the code below:

public class ContactDTO
{
   public string Email {get; set;}
   public decimal? ExchangeRate {g开发者_开发知识库et; 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?


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. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消