Link to home
Start Free TrialLog in
Avatar of Philippe Renaud
Philippe RenaudFlag for Canada

asked on

Help with Dictionnary

Hello EE,

I need to count the days intervals in a dictionnary Of(Date, String)

so if row 1 is :     12/01/01/2019,  ABC
row 2 is :   12/01/15/2019, XYZ
row 3 is : 12/02/22/2019, MNP


I need to have results like this somehow:  

15 days
38 days


so there is 15 days interval between row1 and row2 and there is 38 days between dates of row and row 3  (we need also to take into account the 30/31/28 ending days of months)

how can I do that!? Any efficiant ways with LINQ maybe ?
Avatar of Bill Prew
Bill Prew

Sorry if you've already thought of this, but Dictionary objects are not ordered by default.  Are you sorting them before wanting to do what you asked for?  I'm just wondering the use for deltas between items you can't be sure the order of.


»bp
I think you should change to Sorted Dictionary , then a simple iteration will give the intervals
Your dates look a little funny in the test example too, is that just typos?


»bp
Avatar of Philippe Renaud

ASKER

Ok fine I can use "Sorted Dictionnary" but I was wondering if there is an efficiant way after to count between rows

do I just do something like  DateDiff(row2-row1)  ?? and if I reach end of Dict, I do nothing
ahahha typos....sorry.
Just iterate over the Dictionary
Dim PreviousDate as Date 
Dim Inteval as Integer 
For each datekey in SortedDictionary
Interval = datekey  - Previousdate 
PreviousDate =datekey
Next

Open in new window

what if my Dictionnary was created from a LINQ query ?   I do at the end of my query:     ..... .ToDictionary(Function(y) y.Key, Function(z) z.Value)

I dont think I can do   .ToSortedDictionnary  it wont exist
Is the LINQ query that you generate the dictionary from sorting the items in the order you want?  If so then you probably don't need to use a SortDictionary.


»bp
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Once you have the dictionary in the order you want, either by sorting the data via Linq before you load it to the dictionary, or by sorting the dictionary entries after loading the data in, something like this will get the results you described for differences in days.  The first entry doesn't report any days since there is no prior date for it...

            DateTime bot = new DateTime(1900, 1, 1);
            DateTime prior = bot;
            foreach (var e in dict) {
                if (prior == bot) {
                    prior = e.Key;
                } else
                {
                    Console.WriteLine((e.Key - prior).TotalDays + " days");
                    prior = e.Key;
                }
            }

Open in new window


»bp
hi, what I meant by "ending days" is that we have to make sure it counts well either the month finished with "30" or "31" or 28"...


(edited) :   I guess it's not important since if I do something like   date2 - date1  it calculates everything for me
ok im trying your scripts guys...