I’m trying to create a dictionary collection from a single collection below the key in the dictionary is every file of type “k” and the values for each key are files of type “a”. In other words, I’m trying to build a parent-child relationship but the file names are unique and do not denote the relationship between “a” and “k” file types. The only relationship is the file date. For example, file 4 will be a key b/c it’s of type “k” and it’s values will be files 3 and 2 because their file date is greater than file 3’s date. file 1 should not be included as a child of file 4 since it’s of type “k” even though it’s date is greater than file 3.
Single Collection to work with:
IEnumerable<IFile>
file name file type file date
file1 k 2013-01-01
file2 a 2012-03-30
file3 a 2012-02-27
file4 k 2012-02-23
file5 a 2011-03-31
file6 k 2011-02-24
file7 a 2010-08-24
file8 a 2010-03-31
file9 k 2010-02-26
Desired output:
Dictionary<IFile, IEnumerable<IFile>>
key value
file1 none b/c no files of type "a" exist with a date greater than file1
file4 file3, file2
file6 file5
file9 file8, file7
You could do something like:
That wouldn’t quite get what you want though – because the entry for 2010-02-26 would include all of the future ones. So it’s not just a case of this relationship:
It sounds like it’s actually:
That’s going to be trickier. You might want something like:
That’s if you want to be really LINQ-y. It would be more efficient to walk through both keys and values ordered by date though: