public static string WorkoutDeltaStringForUpdate(FinanceAccountExtraAS oldAcnt, FinanceAccountExtraAS 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 == null) { listHeaderSqls.Add("[" + dbfield + "] = NULL"); } else { 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 { listHeaderSqls.Add("[" + dbfield + "] = " + diff.Value.ToString()); } } } return(listHeaderSqls.Count == 0 ? String.Empty : (@"UPDATE [dbo].[t_fin_account_ext_as] SET " + string.Join(",", listHeaderSqls) + " WHERE [ACCOUNTID] = " + oldAcnt.AccountID.ToString())); }
public static Dictionary <String, Object> WorkoutDeltaForUpdate(FinanceAccountExtraAS oldAcnt, FinanceAccountExtraAS 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(FinanceAccountExtraAS); PropertyInfo[] listProperties = t.GetProperties(); var listSortedProperties = listProperties.OrderBy(o => o.Name); foreach (PropertyInfo item in listSortedProperties) { if (item.Name == "RefDocForSold") { if (oldAcnt.RefenceSoldDocumentID.HasValue) { if (newAcnt.RefenceSoldDocumentID.HasValue) { if (oldAcnt.RefenceSoldDocumentID.Value != newAcnt.RefenceSoldDocumentID.Value) { dictDelta.Add(item.Name, newAcnt.RefenceSoldDocumentID.Value); } } else { dictDelta.Add(item.Name, null); } } else { if (newAcnt.RefenceSoldDocumentID.HasValue) { dictDelta.Add(item.Name, newAcnt.RefenceSoldDocumentID.Value); } } } else { object oldValue = item.GetValue(oldAcnt, null); object newValue = item.GetValue(newAcnt, null); 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); }