this is ok, see the image below at this link:
http://imagedrop.co/img-52c4d454f3427
The Testtime of 19112 is being retrieved properly.
But when I change to another SystemType = 0013-047, the Testtime is zero. But LinqPad does display the Testtime value of 14519.
see image at
http://imagedrop.co/img-52c4d6de456b7
I really cannot see why, would appreciate any useful help.
Code as below:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Data; using System.Globalization; using System.IO; using System.ComponentModel; using System.Windows.Threading; namespace TestSystemUtilisation { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { string path1 = @"C:\DriveD\TESTS4WORK\TestSystemUtilisation\CostCenter\"; string path = @"C:\DriveD\TESTS4WORK\TestSystemUtilisation\"; string outputfilename = "Utilisation.txt"; string[] CostCenterName = { "Analog", "Digital", "INT1", "INT2", "INT3", "Photo", "US-PF", "US-PXS"}; string[] CostCenterNumber = { "130300", "130400", "130500", "130600", "130650", "120200", "120300", "120301"}; private BackgroundWorker worker = new BackgroundWorker(); public MainWindow() { InitializeComponent(); worker.WorkerReportsProgress = true; worker.WorkerSupportsCancellation = true; worker.DoWork += new DoWorkEventHandler(bw_DoWork); worker.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); } private void Window_Loaded(object sender, RoutedEventArgs e) { string[] formats = { "d/M/yyyy hh:mm tt" }; var defaultDT_Start = DateTime.ParseExact("2/1/2013 07:30 AM", formats, new CultureInfo("en-US"), DateTimeStyles.None); DateTimePicker_Start.Value = defaultDT_Start; var defaultDT_End = DateTime.ParseExact("3/1/2013 05:00 PM", formats, new CultureInfo("en-US"), DateTimeStyles.None); DateTimePicker_End.Value = defaultDT_End; Update_ListBoxTestSystemsDefault(); string[] hr = { "Whole Day", "Day Shift w/o OT", "Night Shift w/o OT", "Day Shift with OT", "Night Shift with OT","Day Shift w/o OT + Night Shift w/o OT", "Day Shift w/o OT + Night Shift with OT","Night Shift w/o OT + Day Shift with OT", "Day Shift with OT + Night Shift with OT"}; comboBox_Base.ItemsSource = hr.ToList(); comboBox_CostCenter.ItemsSource = CostCenterName.ToList(); } private void Update_ListBoxTestSystemsDefault() { listBox_TestSystems.Items.Clear(); string outputfilename = "Analog.txt"; FileInfo file = new FileInfo(string.Concat(path1, outputfilename)); StreamReader stRead = file.OpenText(); while (!stRead.EndOfStream) { listBox_TestSystems.Items.Add(stRead.ReadLine()); } stRead.Close(); } private void comboBox_CostCenter_SelectionChanged(object sender, SelectionChangedEventArgs e) { int selectedIndex = comboBox_CostCenter.SelectedIndex; listBox_TestSystems.Items.Clear(); label_CostCenter.Content = CostCenterNumber[selectedIndex]; FileInfo file = new FileInfo(string.Concat(path1, CostCenterName[selectedIndex],".txt")); StreamReader stRead = file.OpenText(); while (!stRead.EndOfStream) { listBox_TestSystems.Items.Add(stRead.ReadLine()); } stRead.Close(); } private void comboBox_Base_SelectionChanged(object sender, SelectionChangedEventArgs e) { int selectedIndex = comboBox_Base.SelectedIndex; string[] hr = { "24", "8.8", "8.5", "11.55", "11.5", "17.3", "20.3", "20.05", "23.05" }; string[] duration = { "00:00 AM - 11:59 PM", "07:30 AM - 05:00 PM", "11:00 PM - 07:45 AM", "07:30 AM - 08:00 PM", "08:00 PM - 07:45 AM","07:30 AM - 05:00 PM + 11:00 PM - 07:45 AM", "07:30 AM - 05:00 PM + 08:00 PM - 07:45 AM", "11:00 PM - 07:45 AM + 07:30 AM - 08:00 PM","07:30 AM - 08:00 PM + 08:00 PM - 07:45 AM" }; if (comboBox_Base.SelectedIndex == selectedIndex) label_Base.Content = hr[selectedIndex]; label_Duration.Content = duration[selectedIndex]; } private void Update_ListBoxUtilisation() { listBox_Utilisation.Items.Clear(); FileInfo file = new FileInfo(string.Concat(path, outputfilename)); StreamReader stRead = file.OpenText(); while (!stRead.EndOfStream) { listBox_Utilisation.Items.Add(stRead.ReadLine()); } stRead.Close(); } private void Query_Click(object sender, RoutedEventArgs e) { textBox_Status.Text = ""; textBox_Status.Text = "In progress ..."; listBox_Utilisation.Items.Clear(); if (worker.IsBusy != true) { worker.RunWorkerAsync(); } } private void bw_DoWork(object sender, DoWorkEventArgs e) { TS_UtilisationDataSetTableAdapters.PPL_TESTSYSTEMS_UTILISATIONTableAdapter adapter = new TS_UtilisationDataSetTableAdapters.PPL_TESTSYSTEMS_UTILISATIONTableAdapter(); TS_UtilisationDataSet.PPL_TESTSYSTEMS_UTILISATIONDataTable table = adapter.GetData(); StreamWriter sw = new StreamWriter(string.Concat(path, outputfilename)); string header1 = "SystemType Testtime Waittime Systemtime Utilisation (%) Utilisation (%)"; string header2 = " (Test, System time) (Test, Wait, System time)"; string header3 = "========== ======== ======== ========== =================== ========================="; sw.WriteLine(header1); sw.WriteLine(header2); sw.WriteLine(header3); var SelectedTestSystems = new List<string>(); var Testtime = new List<Int32>(); var Waittime = new List<Int32>(); var Systemtime = new List<Int32>(); var Utilisation1 = new List<double>(); var Utilisation2 = new List<double>(); Int32 Testtime_hr, Testtime_min; Int32 Waittime_hr, Waittime_min; Int32 Systemtime_hr, Systemtime_min; worker.ReportProgress(10); System.Threading.Thread.Sleep(1000); Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { string[] formats = { "d/M/yyyy hh:mm:ss tt" }; var Start = DateTime.ParseExact(DateTimePicker_Start.Text.ToString(), formats, new CultureInfo("en-US"), DateTimeStyles.None); var End = DateTime.ParseExact(DateTimePicker_End.Text.ToString(), formats, new CultureInfo("en-US"), DateTimeStyles.None); foreach (var arr in listBox_TestSystems.SelectedItems) { SelectedTestSystems.Add(arr.ToString()); } TimeSpan ts = End - Start; int DifferenceInDays = ts.Days; double DifferenceInHours = ts.Hours + ts.Minutes / 60.0; for (int i = 0; i < SelectedTestSystems.Count(); i++) { Testtime.Clear(); Waittime.Clear(); Systemtime.Clear(); Utilisation1.Clear(); Utilisation2.Clear(); for (int d = 0; d <= DifferenceInDays; d++) { DateTime START = Start.AddDays(d); DateTime END = Start.AddDays(d).AddHours(DifferenceInHours); IEnumerable<DataRow> query = from row in table.AsEnumerable() where (row.Field<string>("SYSTEMTYPE") == SelectedTestSystems[i]) && (row.Field<DateTime?>("TS_START") > START) && (row.Field<DateTime?>("TS_END") < END) select row; var TimeTest = query.Select(row => row.Field<Decimal?>("TIME_TEST")).Sum(); var TimeWait = query.Select(row => row.Field<Decimal?>("TIME_WAIT")).Sum(); var TimeSystem = query.Select(row => row.Field<Decimal?>("TIME_SYSTEM")).Sum(); decimal? hr_per_day = Convert.ToDecimal(label_Base.Content.ToString()); decimal? Utilisation_1 = (TimeTest + TimeSystem) * 100 / (hr_per_day * 3600); decimal? Utilisation_2 = (TimeTest + TimeSystem + TimeWait) * 100 / (hr_per_day * 3600); Testtime.Add(Convert.ToInt32(TimeTest)); Waittime.Add(Convert.ToInt32(TimeWait)); Systemtime.Add(Convert.ToInt32(TimeSystem)); Utilisation1.Add(Convert.ToDouble(Utilisation_1)); Utilisation2.Add(Convert.ToDouble(Utilisation_2)); } Dispatcher.Invoke((Action)delegate() { worker.ReportProgress(60); System.Threading.Thread.Sleep(5000); }); Testtime_hr = Testtime.Sum() / 3600; Testtime_min = Testtime.Sum() / 60 - Testtime_hr * 60; Waittime_hr = Waittime.Sum() / 3600; Waittime_min = Waittime.Sum() / 60 - Waittime_hr * 60; Systemtime_hr = Systemtime.Sum() / 3600; Systemtime_min = Systemtime.Sum() / 60 - Systemtime_hr * 60; sw.WriteLine("{0,8}{1,13}{2,12}{3,13}{4,16}{5,26}", SelectedTestSystems[i], string.Concat(Testtime_hr, "h ", Testtime_min, "min"), string.Concat(Waittime_hr, "h ", Waittime_min, "min"), string.Concat(Systemtime_hr, "h ", Systemtime_min, "min"), string.Format("{0:N2}",Utilisation1.Average()), string.Format("{0:N2}",Utilisation2.Average())); } sw.Close(); Update_ListBoxUtilisation(); })); } private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if ((e.Cancelled == true)) { textBox_Status.Text = "Canceled!"; } else if (!(e.Error == null)) { textBox_Status.Text = ("Error: " + e.Error.Message); } else { textBox_Status.Text = "Completed."; } } private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) { textBox_Status.Text = (e.ProgressPercentage.ToString() + "%"); } } }