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 ?
LVL 1
PhilippeRenaudAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
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 EngineerCommented:
I think you should change to Sorted Dictionary , then a simple iteration will give the intervals
Bill PrewIT / Software Engineering ConsultantCommented:
Your dates look a little funny in the test example too, is that just typos?


»bp
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

PhilippeRenaudAuthor 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
PhilippeRenaudAuthor Commented:
ahahha typos....sorry.
John TsioumprisSoftware & Systems EngineerCommented:
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

PhilippeRenaudAuthor 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 ConsultantCommented:
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
kaufmed   (⌐■_■)Shot Through the Heart, and You're to Blame, You Give vars a Bad NameCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
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
PhilippeRenaudAuthor 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
PhilippeRenaudAuthor Commented:
ok im trying your scripts guys...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
LINQ

From novice to tech pro — start learning today.