我需要編寫一個腳本,它將一個列表與一個詞典合併在一起,以創建第三個詞典。我對編程相當陌生,並且在這裏苦於基本知識。使用詞典
到目前爲止,我創建了以下類,它生成一個日期列表。我有另一個生成字典的類,我想基本上創建第三個字典,其中包含第一個列表中不存在的日期和數據。 任何想法我應該如何做到這一點?謝謝。
class StartList: IDisposable
{
private readonly string[] names = new[] { "name1", "name2", "name3"};
private SqlConnection conn;
private Dictionary<string, List<DateTime>> startData = new Dictionary<string, List<DateTime>>();
public StartList()
{
this.conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NameCon"].ConnectionString);
this.conn.Open();
}
private void Dispose()
{
if (this.conn != null)
{
if (this.conn.State != ConnectionState.Closed)
{
try
{
this.conn.Close();
}
catch
{
}
}
this.conn.Dispose();
this.conn = null;
}
}
public void ImportStartData()
{
foreach (string name in this.names)
{
this.startData.Add(name, this.ImportStartData(name));
}
}
public List<DateTime> ImportStartData(string name)
{
List<DateTime> result = new List<DateTime>();
string sqlCommand = string.Format("SELECT * FROM {0}_Index ", name);
using (SqlCommand cmd = new SqlCommand(sqlCommand, this.conn))
{
cmd.CommandType = CommandType.Text;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
result.Add(reader.GetDateTime(0));
}
}
}
return result;
}
}
你的方法「ImportStartData」是奇怪的。該方法遍歷「this.names」中的條目,然後繼續將條目添加到自己的「this.names」中。 – 2010-09-14 14:01:20
和'this.names'是'readonly'。我不認爲我理解。 – recursive 2010-09-14 14:18:58
對不起,我在ImportStartData中犯了一個錯誤,應該已經添加到startData中,而不是名稱。謝謝 – Brian 2010-09-14 14:29:33