How to compare two properties in CRM 2011

Posted on Posted in CRM 2011 SDK, MS CRM 2011, MS Dynamics, MS Dynamics CRM

How to compare two properties in CRM 2011
One from actual entity and second manually/newly created and then compare if different or same


///
/// To compare two properties and return result either true or false
///

///existing value of property in the database for entity ///parameter name ///new value generated or need to compare /// true if both same or false if dont match
static public bool CompareProperty(Microsoft.Xrm.Sdk.Entity current, string newkey, object newvalue)
{
if (newvalue == null || string.IsNullOrEmpty(newkey))
return false;

if (!current.Contains(newkey))
return false;

object currentvalue = current[newkey];

if (newvalue is bool)
{
bool propReady = (bool)newvalue;
return propReady = (bool)currentvalue;
}
else if (newvalue is EntityReference)
{
EntityReference propReady = (EntityReference)newvalue;

return propReady.Id == ((EntityReference)currentvalue).Id;
}
else if (newvalue is DateTime)
{
DateTime propReady = (DateTime)newvalue;
return propReady == (DateTime)currentvalue;
}
else if (newvalue is decimal)
{
Decimal propReady = (decimal)newvalue;
return propReady == (decimal)currentvalue;
}
else if (newvalue is float)
{
float propReady = (float)newvalue;
return propReady == (float)currentvalue;
}
else if (newvalue is int)
{
int propReady = (int)newvalue;
return propReady == (int)currentvalue;
}
else if (newvalue is Money)
{
Money propReady = (Money)newvalue;
return propReady == (Money)currentvalue;
}
else if (newvalue is Guid)
{
Guid propReady = (Guid)newvalue;
return propReady == (Guid)currentvalue;
}
else if (newvalue is string)
{
string propReady = (string)newvalue;
return propReady == (string)currentvalue;
}
else if (newvalue is OptionSetValue)
{
OptionSetValue propReady = (OptionSetValue)newvalue;
return propReady.Value == ((OptionSetValue)currentvalue).Value;
}
else
return false;
}