views:

90

answers:

1

Hi, I need to write a script which merges a list with a dictionary to create a third dictionary. I'm pretty new to programming and am struggling with the basics here.

So far I've created the following class which generates a list of dates. I have another class which generates a dictionary and I want to basically create a third dictionary which contains the dates and data which do not exist already in the first list. Any ideas how I should do this? Thanks.

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;
    }

}
A: 

First you need to modify the below code block From:

 public void ImportStartData()
    {
        foreach (string name in this.names)
        {
            this.names.Add(name, this.ImportStartData(name));
        }
    }

To:

public void ImportStartData()
    {
        foreach (string name in this.names)
        {
            if(!startData.ContainsKey(name)) //If this check is not done, then Dictionary will throw, duplicate key exception.
            {
               this.startData.Add(name, this.ImportStartData(name));
            }
        }
    }

Anyway, the better approach would be, if possible first read the name as well as Date from database, possibly into a DataTable and then using LINQ/foreach loop, group the results by name.

Siva Gopal
Thanks for the heads up Siva.
Brian
I'm not sure about your suggestion. I probably didn't explain the goal properly. I am ultimately updating a number of SQL tables with new data gathered from an Excel sheet. I start by pulling the existing data from SQL and then I pull the excel data into dictionaries. Then I take the new dictionaries and remove the old data which already exists in the List which we populated from SQL.
Brian
Do you mean that you want to merge the existing SQL data with the data from Excel sheet and then write the merged data back into SQL tables ?
Siva Gopal
Not exactly, I just want a third dictionary containing only the new data, then I import that to sql.
Brian
So the excel and sql will be identical except for one or two new entries on the excel file.
Brian