比较sql server中的钱和C#中的十进制

时间:2011-11-14 18:04:41

标签: c# sql-server entity-framework entity-framework-4.1

我在db中有一些类型为money的字段,在C#中我使用了十进制数字。我正在比较C#c ode中的值:

public static string CompareValues(DbPropertyValues currentValues, DbPropertyValues DbValues)
        {
            string result = string.Empty;

            foreach (var currentPropertyName in currentValues.PropertyNames)
            {
                foreach (var DBPropertyName in DbValues.PropertyNames)
                {
                    if (DBPropertyName == "PropertyCreditLimit" || DBPropertyName == "PropertyCreditLimit" || DBPropertyName == "PropertyCreditLimit" || DBPropertyName == "PropertyCreditLimit" || DBPropertyName == "PropertyCreditLimit")


                    if ((currentPropertyName == DBPropertyName))
                    {
                        if (( currentValues[currentPropertyName] != null && DbValues != null) &&  (currentValues[currentPropertyName].ToString() != DbValues[DBPropertyName].ToString()))
                        {
                            result += string.Format("Property{0}, {1} =  {2}, {3} <br />", currentPropertyName, DbValues[DBPropertyName], currentValues[currentPropertyName], DBPropertyName);
                        }
                    }
                }
            }

            return result;
        }

但由于零/精度的数量不匹配:

PropertyDateFirstReported, 3/8/2008 1:15:36 AM = 1/1/0001 12:00:00 AM, DateFirstReported 
PropertyCreditLimit, 564000.0000 = 564000.00, CreditLimit 
PropertyBalance, 45.0000 = 45.00, Balance 
PropertyMinimumInstallment, 0.0000 = 0.00, MinimumInstallment 
PropertyTerm, = Term1, Term 
PropertyPurpose, a = a1, Purpose 
PropertyCollateralValue, 0.0000 = 0.00, CollateralValue 
PropertyAccountUniqueID, 3767 = 0, AccountUniqueID 
PropertyPayment, 564000.0000 = 564000.00, Payment

如何解决这个问题?

5 个答案:

答案 0 :(得分:0)

 (currentValues[currentPropertyName].ToString() != DbValues[DBPropertyName].ToString()

您目前正在将它们作为字符串进行比较,这解释了为什么没有匹配 - 您必须直接比较这些值。

答案 1 :(得分:0)

如果必须进行字符串比较,请使用'f2'格式化程序来保证2个小数位。否则,只需将DB值转换为(decimal)

可能是这样的:

bool isEqual = false;
if ( currentValues[currentPropertyName] != null && DbValues != null) {
 if (currentValues[currentPropertyName] is decimal && DbValues[DBPropertyName] is decimal) {
   isEqual = (Decimal)currentValues[currentPropertyName] == (Decimal)DbValues[DBPropertyName];
} else {
   isEqual = currentValues[currentPropertyName].ToString() == DbValues[DBPropertyName].ToString();
}

如果找到ToString()没有为您提供正确的比较值的其他数据类型,则可以添加其他else子句。或者您可以添加它们以避免ToString()调用。

答案 2 :(得分:0)

如果您要将它们作为字符串进行比较,您将需要它们采用相同的格式,例如

的ToString(&#34; F02&#34;,CultureInfo.InvariantCulture);

为什么不在数据库的读/写上将Money转换为Decimal,你将会在这个地方到处都是。

答案 3 :(得分:0)

试试这个:

if (!object.Equals(currentValues[currentPropertyName], DbValues[DBPropertyName]))
{
  //Do stuff
}

说明: object.Equals(null,null)被认为是真的

答案 4 :(得分:0)

不要转换为字符串来比较小数;空值,填充,格式,本地化,有效数字,小数位,都可以在运行时更改。您在代码中种植地雷。这样做。

decimal? d1 = currentValues[currentPropertyName] as Nullable<decimal>;
decimal? d2 = (DbValues == null)? null : DbValues[DBPropertyName] as Nullable<decimal>;
if (d1 == d2)
    ...;
相关问题