We help IT Professionals succeed at work.

Help with Dictionnary

Philippe Renaud
on
91 Views
Last Modified: 2019-03-05
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 ?
Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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 TsioumprisIT Supervisor
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
I think you should change to Sorted Dictionary , then a simple iteration will give the intervals
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

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


»bp
Philippe RenaudVice President

Author

Commented:
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 RenaudVice President

Author

Commented:
ahahha typos....sorry.
John TsioumprisIT Supervisor
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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

Philippe RenaudVice President

Author

Commented:
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 PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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
Philippe RenaudVice President

Author

Commented:
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 RenaudVice President

Author

Commented:
ok im trying your scripts guys...

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions