Help with Dictionnary

PhilippeRenaud
PhilippeRenaud used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewIT / Software Engineering Consultant
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 TsioumprisSoftware & Systems Engineer

Commented:
I think you should change to Sorted Dictionary , then a simple iteration will give the intervals
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

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


»bp
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

Author

Commented:
ahahha typos....sorry.
John TsioumprisSoftware & Systems Engineer

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

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 PrewIT / Software Engineering Consultant
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
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
What do you mean by, "we need also to take into account the 30/31/28 ending days of months?"

Outside of that, you can sort the keys themselves:

Dim rand As New Random()
Dim query = From letter In "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            Let randomOffset = rand.Next(1, 10000)
            Let randomDate = DateTime.Now.AddDays(randomOffset)
            Select New With
            {
                letter,
                randomDate
            }
			
Dim dictionary = query.ToDictionary(Function(item) item.randomDate, Function(item) item.letter)

For Each value In dictionary.Keys.OrderBy(Function(key) key)
	Console.WriteLine(value)
Next

Open in new window

Bill PrewIT / Software Engineering Consultant
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

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

Author

Commented:
ok im trying your scripts guys...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial