Click here to Skip to main content
15,996,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a very difficult question to ask, hopefully it will make sense. I am reading two different types of text files and checking the records within a dictionary. The textfiles can have different dates so I need to dump the dictionary after it checks one file and read again through database to fetch the new date of the textfile. How do I dumb the dictionary after every run?

What I have tried:

private Dictionary<DateTime, Dictionary<string, long>> DataByNum;
       private Dictionary<DateTime, Dictionary<string, long>> DataByID;

       private Dictionary<string, long> GetNumData(DateTime DailyFileDate)
       {
           DailyFileDate = DailyFileDate.Date;
           if (DataByNum == null || !DataByNum.ContainsKey(DailyFileDate))
           {
               CacheData(DailyFileDate);
           }
           return DataByNum[DailyFileDate];
       }

       private Dictionary<string, long> GetIDData(DateTime DailyFileDate)
       {
           DailyFileDate = DailyFileDate.Date;
           if (DataByID == null || !DataByID.ContainsKey(DailyFileDate))
           {
               CacheData(DailyFileDate);
           }
           return DataByID[DailyFileDate];
       }

       private void CacheData(DateTime DailyFileDate)
       {
           DailyFileDate = DailyFileDate.Date;

           if (DataByNum == null)
           {
               DataByNum = new Dictionary<DateTime, Dictionary<string, long>>();
           }
           if (DataByID == null)
           {
               DataByID = new Dictionary<DateTime, Dictionary<string, long>>();
           }


           var result = ExtractData(DailyFileDate);


           foreach (DataRow row in result)
           {
               var date = DateTime.Parse(row.GetString("FileDate")).Date;
               if (!DataByNum.ContainsKey(date))
               {
                   DataByNum[date] = new Dictionary<string, long>();
               DataByNum[date].Add(row.GetString("Num"), row.GetLong("RID").Value);
               }
               if (!DataByID.ContainsKey(date))
               {
                   DataByID[date] = new Dictionary<string, long>();
               DataByID[date].Add(row.GetString("CustomerID"), row.GetLong("RID").Value);
               }
           }
Posted
Updated 10-Feb-17 7:57am
Comments
[no name] 10-Feb-17 13:17pm    
yourDictionary.Clear()?
SmartDeveloping 10-Feb-17 13:23pm    
Is there a way I can only build one dictionary according to which file I am reading?
[no name] 10-Feb-17 13:41pm    
Since your dictionary appears to be the same I don't see why you think you need to have more than one.
SmartDeveloping 10-Feb-17 13:52pm    
Yeah I'm having trouble converting that into one.

1 solution

You can either call dictionary.Clear(), or simply re-instantiate it with new.
 
Share this answer
 
Comments
SmartDeveloping 10-Feb-17 14:06pm    
Is there a way I can load only one dictionary at a time? At this time It is loading both.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900