private static void UpdateClassBinColumn( DataStore store, Project project, Scenario scenario, string classTypeDatasheetName, string classTypeFrequencyColumnName, string classTypeMaximumColumnName, string classGroupDatasheetName, string classGroupMaximumColumnName, string outputDatasheetName, string outputDatasheetMinimumColumnName, string outputDatasheetMaximumColumnName, string outputDatasheetClassColumnName) { List <ClassBinDescriptor> e = GetClassBinDescriptors( project, classTypeDatasheetName, classTypeFrequencyColumnName, classTypeMaximumColumnName, classGroupDatasheetName, classGroupMaximumColumnName); if (e == null) { return; } if (!OutputDataExists( store, outputDatasheetName, Strings.DATASHEET_SCENARIOID_COLUMN_NAME, scenario)) { return; } if (!OutputDataExists( store, outputDatasheetName, outputDatasheetMinimumColumnName, scenario)) { return; } StringBuilder sb = new StringBuilder(); Debug.Assert(!(e.ElementAtOrDefault(e.Count() - 1).Maximum.HasValue)); if (e.Count() > 1) { sb.AppendFormat(CultureInfo.InvariantCulture, "UPDATE [{0}] SET {1} = CASE", outputDatasheetName, outputDatasheetClassColumnName); for (int i = 0; i < e.Count(); i++) { ClassBinDescriptor d = e.ElementAtOrDefault(i); if (d.Maximum.HasValue) { Debug.Assert(i < e.Count() - 1); sb.AppendFormat(CultureInfo.InvariantCulture, " WHEN {0} >= {1} And {2} <= {3} THEN {4}", outputDatasheetMinimumColumnName, d.Minimum, outputDatasheetMaximumColumnName, d.Maximum.Value, d.Minimum); } else { Debug.Assert(i == e.Count() - 1); sb.AppendFormat(CultureInfo.InvariantCulture, " WHEN {0} >= {1} THEN {2}", outputDatasheetMinimumColumnName, d.Minimum, d.Minimum); } } sb.AppendFormat(" WHEN {0} IS NULL THEN {1}", outputDatasheetMaximumColumnName, e.Last().Minimum); sb.Append(" END"); } else { sb.AppendFormat(CultureInfo.InvariantCulture, "UPDATE [{0}] SET {1} = {2}", outputDatasheetName, outputDatasheetClassColumnName, e.ElementAtOrDefault(0).Minimum); } sb.AppendFormat(CultureInfo.InvariantCulture, " WHERE ScenarioID = {0}", scenario.Id); store.ExecuteNonQuery(sb.ToString()); }
public static List <ClassBinDescriptor> GetClassBinGroupDescriptors( Project project, string datasheetName, string maximumColumnName) { DataTable dt = project.GetDataSheet(datasheetName).GetData(); DataView dv = new DataView(dt, null, null, DataViewRowState.CurrentRows); if (dv.Count == 0) { return(null); } List <ClassBinDescriptor> lst = new List <ClassBinDescriptor>(); Dictionary <int, bool> dict = new Dictionary <int, bool>(); foreach (DataRowView drv in dv) { int value = Convert.ToInt32(drv[ maximumColumnName], CultureInfo.InvariantCulture); if (!dict.ContainsKey(value)) { lst.Add(new ClassBinDescriptor(value, value)); dict.Add(value, true); } } lst.Sort((ClassBinDescriptor d1, ClassBinDescriptor d2) => { return(d1.Minimum.CompareTo(d2.Minimum)); }); int Prev = 0; for (int i = 0; i < lst.Count; i++) { ClassBinDescriptor d = lst[i]; int t = d.Minimum; d.Minimum = Prev; Prev = t + 1; } ClassBinDescriptor last = lst[lst.Count - 1]; lst.Add(new ClassBinDescriptor(last.Maximum.Value + 1, null)); #if DEBUG Debug.Assert(lst.Count > 0); foreach (ClassBinDescriptor ad in lst) { if (ad.Maximum.HasValue) { Debug.Assert(ad.Minimum <= ad.Maximum.Value); } } Debug.Assert(lst[lst.Count - 1].Maximum == null); #endif return(lst); }