c#多维数组
本文关键字:数组 | 更新日期: 2023-09-27 18:12:03
我对自己的工作有点困惑,我似乎把我的问题复杂化了。
我正在从呼叫拨号器中提取数据,这个拨号器记录所有代理的所有呼叫,每个代理都在一个队列中,可以有多个代理在同一个队列中。
我在SQL i中的基本计算可以提取日期,队列,小时数和每小时的呼叫数,如下所示:
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;Password=d@t0r@.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);
}
}
我真的不知道你想要达到什么目的,但是像这样的东西可能会有帮助。
Dictionary<DateTime, Dictionary<string, KeyValuePair<int,int>>> dicData
DateTime是你的DateTime, string是你的Queue和int,在KeyValuePair中的int是小时和numberOfCall对。
编辑1:实际上它必须是List of 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));
}
EDIT
那么,在你的readingwhile循环中你可以输入
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);