public static string WorkoutDeltaStringForUpdate(FinanceAccountExtraLoan oldAcnt, FinanceAccountExtraLoan newAcnt) { var diffs = WorkoutDeltaForUpdate(oldAcnt, newAcnt); List <String> listHeaderSqls = new List <string>(); foreach (var diff in diffs) { var dbfield = newAcnt.GetDBFieldName(diff.Key); if (diff.Value is DateTime) { listHeaderSqls.Add("[" + dbfield + "] = '" + ((DateTime)diff.Value).ToString("yyyy-MM-dd") + "'"); } else if (diff.Value is Boolean) { listHeaderSqls.Add("[" + dbfield + "] = " + (((Boolean)diff.Value) ? "1" : "NULL")); } else if (diff.Value is String) { if (String.IsNullOrEmpty((string)diff.Value)) { listHeaderSqls.Add("[" + dbfield + "] = NULL"); } else { listHeaderSqls.Add("[" + dbfield + "] = N'" + diff.Value + "'"); } } else if (diff.Value is Decimal) { if (Decimal.Compare((Decimal)diff.Value, 0) == 0) { listHeaderSqls.Add("[" + dbfield + "] = NULL"); } else { listHeaderSqls.Add("[" + dbfield + "] = " + diff.Value.ToString()); } } else if (diff.Value is Boolean) { if ((bool)diff.Value) { listHeaderSqls.Add("[" + dbfield + "] = 1"); } else { listHeaderSqls.Add("[" + dbfield + "] = 0"); } } else { if (diff.Value == null) { listHeaderSqls.Add("[" + dbfield + "] = NULL"); } else { listHeaderSqls.Add("[" + dbfield + "] = " + diff.Value.ToString()); } } } return(listHeaderSqls.Count == 0 ? String.Empty : (@"UPDATE [dbo].[t_fin_account_ext_loan] SET " + string.Join(",", listHeaderSqls) + " WHERE [ACCOUNTID] = " + oldAcnt.AccountID.ToString())); }
public static Dictionary <String, Object> WorkoutDeltaForUpdate(FinanceAccountExtraLoan oldAcnt, FinanceAccountExtraLoan newAcnt) { Dictionary <String, Object> dictDelta = new Dictionary <string, object>(); if (oldAcnt == null || newAcnt == null || Object.ReferenceEquals(oldAcnt, newAcnt) || oldAcnt.AccountID != newAcnt.AccountID) { throw new ArgumentException("Invalid inputted parameter Or AccountID change is not allowed"); } if (!oldAcnt.IsValid() || !newAcnt.IsValid()) { throw new Exception("Account info is invalid"); } Type t = typeof(FinanceAccountExtraLoan); PropertyInfo[] listProperties = t.GetProperties(); var listSortedProperties = listProperties.OrderBy(o => o.Name); foreach (PropertyInfo item in listSortedProperties) { if (item.Name == "LoanTmpDocs") { continue; } object oldValue = item.GetValue(oldAcnt, null); object newValue = item.GetValue(newAcnt, null); if (item.PropertyType == typeof(Nullable <Decimal>)) { if (Nullable.Compare <Decimal>(oldValue as Nullable <Decimal>, newValue as Nullable <Decimal>) != 0) { if (newValue != null) { dictDelta.Add(item.Name, (newValue as Nullable <Decimal>).Value); } else { dictDelta.Add(item.Name, null); } } } else if (item.PropertyType == typeof(Nullable <DateTime>)) { if (Nullable.Compare <DateTime>(oldValue as Nullable <DateTime>, newValue as Nullable <DateTime>) != 0) { if (newValue != null) { dictDelta.Add(item.Name, (newValue as Nullable <DateTime>).Value); } else { dictDelta.Add(item.Name, null); } } } else if (item.PropertyType == typeof(Nullable <Boolean>)) { if (Nullable.Compare <Boolean>(oldValue as Nullable <Boolean>, newValue as Nullable <Boolean>) != 0) { if (newValue != null) { dictDelta.Add(item.Name, (newValue as Nullable <Boolean>).Value ? 1 : 0); } else { dictDelta.Add(item.Name, null); } } } else if (item.PropertyType == typeof(Nullable <Int32>)) { if (Nullable.Compare <Int32>(oldValue as Nullable <Int32>, newValue as Nullable <Int32>) != 0) { if (newValue != null) { dictDelta.Add(item.Name, (newValue as Nullable <Int32>).Value); } else { dictDelta.Add(item.Name, null); } } } else if (item.PropertyType == typeof(Nullable <Int16>)) { if (Nullable.Compare <Int16>(oldValue as Nullable <Int16>, newValue as Nullable <Int16>) != 0) { if (newValue != null) { dictDelta.Add(item.Name, (newValue as Nullable <Int16>).Value); } else { dictDelta.Add(item.Name, null); } } } else if (item.PropertyType == typeof(Decimal)) { if (Decimal.Compare((Decimal)oldValue, (Decimal)newValue) != 0) { dictDelta.Add(item.Name, newValue); } } else if (item.PropertyType == typeof(String)) { if (String.CompareOrdinal((string)oldValue, (string)newValue) != 0) { dictDelta.Add(item.Name, newValue); } } else if (item.PropertyType == typeof(DateTime)) { if (DateTime.Compare(((DateTime)oldValue).Date, ((DateTime)newValue).Date) != 0) { dictDelta.Add(item.Name, newValue); } } else { if (!Object.Equals(oldValue, newValue)) { dictDelta.Add(item.Name, newValue); } } } return(dictDelta); }