Avatar of Philippe Renaud
Philippe Renaud
Flag 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 ?
* LINQVisual Basic.NETC#

Avatar of undefined
Last Comment
Philippe Renaud

8/22/2022 - Mon
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
John Tsioumpris

I think you should change to Sorted Dictionary , then a simple iteration will give the intervals
Bill Prew

Your dates look a little funny in the test example too, is that just typos?


»bp
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
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
Philippe Renaud

ASKER
ahahha typos....sorry.
John Tsioumpris

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Philippe Renaud

ASKER
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
Bill Prew

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
kaufmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Bill Prew

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
Your help has saved me hundreds of hours of internet surfing.
fblack61
Philippe Renaud

ASKER
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
Philippe Renaud

ASKER
ok im trying your scripts guys...