2013-05-03 113 views
0

我對自己的工作有點困惑,我似乎把我的問題弄得很複雜。C#多維數組

我正在將數據從呼叫撥號程序中撥出,此撥號程序記錄所有座席的所有呼叫,並且每個座席都在隊列中,同一隊列中可以有多個座席。

我的SQL基本的計算,我可以拉日期,隊列,時間以及每個小時的呼叫數如下所示:

callDate queueid cHour numberOfCalls 
2013-05-03 No Queue  0   1 
2013-05-03 No Queue  2   1 
2013-05-03 No Queue  6   1 
2013-05-03 No Queue  7   7 
2013-05-03 No Queue  8   6 
2013-05-03 No Queue  9   14 
2013-05-03 No Queue  10   6 
2013-05-03 No Queue  11   5 
2013-05-03 No Queue  12   8 
2013-05-03 17001  7   114 
2013-05-03 17001  8   238 
2013-05-03 17001  9   227 
2013-05-03 17001  10   190 
2013-05-03 17001  11   221 
2013-05-03 17001  12   73 
2013-05-03 17002  6   3 
2013-05-03 17002  7   125 

在那裏,你可以看到隊列,小時和多少電話那一小時(小時上午7點,上午8點...等)。

我需要知道,如果我創建一個多維數組來存儲每個小時的隊列,小時和每個隊列的調用次數(如果這樣做有道理?),以便我可以稍後將它用作圖形?

這裏是我得到卡住了我的示例代碼:

的XAML:

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
    xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
    xmlns:ThemeManager.ThemeName="MetropolisDark" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 

    <DVC:Chart Name="Chart" 
       Background="#463F3F">     
     <DVC:Chart.PlotAreaStyle> 
      <Style TargetType="Grid"> 
       <Setter Property="Background" Value="Transparent" /> 
      </Style> 
     </DVC:Chart.PlotAreaStyle> 
    </DVC:Chart> 

</Grid> 

C#:

private void AllAgentHourData() 
    { 
     string[] queueid = new string[100]; 
     int[] callHour = new int[100]; 
     int count = 0; 
     int counter = 0; 

     SqlConnection sqlConnection1 = new SqlConnection("Server=nl-reportserver;Database=RC_Dailer_WH;User Id=sa;[email protected]@.001"); 
     SqlCommand cmd = new SqlCommand(); 
     SqlDataReader reader; 

     //cmd.CommandText = "SELECT * FROM RC_call_logs WHERE convert(date,call_logdate,120) = convert(date,GETDATE(),120)"; 
     cmd.CommandText = "Select distinct queueid from RC_call_logs order by queueid"; 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = sqlConnection1; 

     sqlConnection1.Open(); 

     reader = cmd.ExecuteReader(); 
     if (reader.HasRows) 
     { 
      while (reader.Read()) 
      { 
       queueid[count] = reader.GetString(0); 
      } 
     } 
     else 
     { 
      MessageBox.Show("No Error message"); 
     } 
     reader.Close(); 

     sqlConnection1.Close(); 

     Random random = new Random(); 

     //Chart is your chart object in Xaml 
     //declare your series 
     for (int i = 1; i < 10; i++) 
     { 
      LineSeries ls = new LineSeries(); 

      ls.Title = i.ToString(); 
      ls.IndependentValueBinding = new Binding("Key"); 
      ls.DependentValueBinding = new Binding("Value"); 

      ls.ItemsSource = new KeyValuePair<DateTime, int>[]{ 
      new KeyValuePair<DateTime,int>(DateTime.Now    , random.Next(1000)), 
      new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(1), random.Next(10, 1000)), 
      new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(2), random.Next(10, 1000)), 
      new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(3), random.Next(10, 1000)), 
      new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(4), random.Next(10, 1000))}; 

      // then add it to the chart 
      Chart.Series.Add(ls); 
     } 

    } 
+0

請仔細閱讀http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title – 2013-05-03 12:22:46

+0

感謝,但我不知道該怎麼這個詞,因此長問題 – Silentdarkness 2013-05-03 12:25:48

+2

不要對任何東西使用多維數組。 – Jodrell 2013-05-03 12:30:48

回答

1

我真的不知道是什麼你正在努力實現,但是這樣的事情可能會有所幫助。

Dictionary<DateTime, Dictionary<string, KeyValuePair<int,int>>> dicData 

日期時間是你的日期時間,字符爲您的隊列和INT,INT在KeyValuePair是小時numberOfCall對。

編輯1:

實際上卻是KeyValuePair列表。這裏有一個例子:

Dictionary<DateTime, Dictionary<string, List<KeyValuePair<int, int>>>> dicData = new Dictionary<DateTime, Dictionary<string, List<KeyValuePair<int, int>>>>(); 
     //dt is your result from SQL query 
     foreach (DataRow dr in dt.Rows) 
     { 
      DateTime dtm = DateTime.Parse(dr["DateTime"].ToString()); 
      string queue = dr["Queue"].ToString(); 
      int hours = int.Parse(dr["Hours"].ToString()); 
      int cycles = int.Parse(dr["Cycles"].ToString()); 

      //Adding Distinct DateTime objects as Key 
      if(!dicData.ContainsKey(dtm)) 
      { 
       dicData[dtm] = new Dictionary<string, KeyValuePair<int, int>>(); 
      } 

      //Adding distinct Queue object as Key under the DateTime dictionary 
      if (!dicData.ContainsKey(queue)) 
      { 
       dicData[dtm][queue] = new List<KeyValuePair<int, int>>(); 
      } 
      dicData[dtm][queue].Add(new KeyValuePair<int, int>(hours, cycles)); 
     } 
+0

這會工作,你能爲我提供以外的答案,幫我一個實際的例子這將是我第一次使用這個,但使得sence。 – Silentdarkness 2013-05-03 12:32:40

+0

我該如何聲明dt? – Silentdarkness 2013-05-03 12:48:38

+0

DataTable dt = new DataTable(); 您可以用SqlDataAdapter填充它可以說:「da」通過: da .Fill(dt); – Xelom 2013-05-03 12:50:32

0

編輯

所以,在你的閱讀while循環,你可以做,

while (reader.Read()) 
{ 
    var callHour = new CallHour(
     DateTime.ParseExact("yyyy-mm-dd", reader.GetString(0)), 
     reader.GetInt32(2), 
     reader.GetInt32(3), 
     reader.IsDBNull(1) ? null : reader.GetInt32(1)); 

    callHours.Add(callHour.Time, callHour); 
} 

做一個類,你可以把它CallHour那麼你可以有一些通用的收集CallHour。該通用集合將支持IEnumerable<CallHour>並可能支持IList<CallHour>


public class CallHour 
{ 
    private readonly DateTime time; 

    public CallHour(
      DateTime day, 
      int hour, 
      int callCount, 
      int? queueId = null) 
    { 
     this.hour = new DateTime(
      day.Year, 
      day.Month, 
      day.Day, 
      hour, 
      0, 
      0, 
      0); 

     this.CallCount = callCount; 
     this.QueueId = queueId; 
    } 

    public DateTime Time 
    { 
     get 
     { 
      return this.time; 
     } 
    } 

    public int? QueueId { get; set; } 
    public int CallCount { get; set; } 
}  

然後,你可以聲明一個排序的列表,並添加您的第一個小時。

var callHours = new SortedList<DateTime, CallHour>(); 

var someCallHour = new CallHour(
    DateTime.Parse("2013-05-03 00:00:00"), 0, 1); 

callHours.Add(someCallHour.Time, someCallHour);