Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:43:26+00:00 2026-06-11T18:43:26+00:00

I’m trying to create a dictionary collection from a single collection below the key

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:43:27+00:00Added an answer on June 11, 2026 at 6:43 pm

    You could do something like:

    var result = data.Where(x => x.Type == 'k')
                     .ToDictionary(x => x.Name,
                                   x => data.Where(a => a.Type == 'a' &&
                                                   a.Date >= x.Date)
                                            .Select(a => a.Name)
                                            .ToList());
    

    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:

    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.

    It sounds like it’s actually:

    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 and their file date is less than file 1’s date.

    That’s going to be trickier. You might want something like:

    var orderedKeys = data.Where(x => x.Type == 'k')
                          .OrderBy(x => x.Date)
                          .Concat(null); // You'll see why in a minute...
    
    // Just for convenience. Could make this more efficient, admittedly.
    var values = data.Where(x => x.Type == 'a').ToList();
    
    var result = orderedKeys.Zip(orderedKeys.Skip(1),
                                 (current, next) => new { current, next })
                            .ToDictionary(pair => pair.current.Name,
         // Sorry for the formatting...
         pair => values.Where(v => v.Date >= pair.current.Date &&
                                   pair.next == null || v.Date < pair.next.Date)
                       .Select(v => v.Name)
                       .ToList());
    

    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:

    var ordered = data.OrderBy(x => x.Date);
    var result = new Dictionary<string, List<string>>();
    var currentList = null;
    foreach (var item in ordered)
    {
        if (item.Type == 'a' && currentList != null)
        {
            currentList.Add(item.Name);
        }
        else if (item.Type == 'k')
        {
            currentList = new List<string>();
            result[item.Name] = currentList;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.