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

  • SEARCH
  • Home
  • 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 3627554
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:54:12+00:00 2026-05-18T23:54:12+00:00

i have this code below that loops through a data structure builds up a

  • 0

i have this code below that loops through a data structure builds up a dictionary.

I have this code duplicated multiple times with the only difference being the Key to the dictionary

so in the below code it happens to be:

  task.Project + task.Name

that is the key to the dictionary but in other cases its just:

 task.Project

or just

 task.Name

here is an example of one of hte hard coded “Bucket” methods.

My goal is to have a generic “Bucket” method where i can have a callback or some way to pass in the function for calculating the key.

What is the best way of doing this ??

private Dictionary<string, TeamHours> BucketByProjectTask(Dictionary<string, TimeBooking> timebookings)
{
    Dictionary<string, TeamHours> dict = new Dictionary<string, TeamHours>();

    foreach (var name in timebookings.Keys)
    {
        TimeBooking tb = timebookings[name];
        Person p = tb.Person;

        foreach (var booking in tb.WeeklyTimeBookings.Keys)
        {
            var item = tb.WeeklyTimeBookings[booking];
            foreach (var task in item.TaskSlices)
            {
                if (dict.ContainsKey(task.Project + task.Name))
                {
                    TeamHours th = dict[task.Project + task.Name];
                    th.Hours = th.Hours + task.Hours;
                }
                else
                {
                    TeamHours th = new TeamHours();
                    th.Hours = task.Hours;
                    th.Project = task.Project;
                    th.Task = task.Name;
                    th.Workstream = tb.Person.OrganisationalUnitName;
                    dict[task.Project + task.Name] = th;
                }
            }

        }

    }
    return dict;
}
  • 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-05-18T23:54:12+00:00Added an answer on May 18, 2026 at 11:54 pm

    Well, you mostly answered the question yourself. Either pass a delegate into the method or inherit different implementations and specialize by implementing an abstract method. Another option is separating the key-building algorithm as an interface, which leads to best separation of concerns, but the overhead might be too much for simple scenarios.

    Option 1 — delegates

    private Dictionary<string, TeamHours> BucketByProjectTask(Dictionary<string, TimeBooking> timebookings, Func<string, Task> getTaskKey)
    {
        …
        dict[getTaskKey(task)] = th;
        …
    }
    

    Good for use in highly-localized scenarios (i.e. implementation and usages private to a single class) with just a few simple key-building expressions.

    Option 2 — abstract class and method

    class abstract BucketAlgorithm
    {
        protected abstract string GetTaskKey(Task task);
    
    
        public  Dictionary<string, TeamHours> BucketByProjectTask(Dictionary<string, TimeBooking> timebookings)
        {
            …
            dict[GetTaskKey(task)] = th;
            …
        }
    }
    
    class SpecificBucketAlgorithm : BucketAlgorithm
    {
        protected override string GetTaskKey(Task task) { … }
    }
    

    Good for use within a medium scope like just one assembly, where there’s no need for better separation of concerns (interfaces from implementation), or where are several non-trivial key-building algorithms required.

    Option 3 — decomposed into an interface

    interface ITaskKeyGenerator
    {
        string GetTaskKey(Task task);
    }
    
    class BucketAlgorithm
    {
        public BucketAlgorithm(ITaskKeyGenerator taskKeyGenerator)
        {
            this.taskKeyGenerator = taskKeyGenerator;
        }
    
        private ITaskKeyGenerator taskKeyGenerator;
    
        public  Dictionary<string, TeamHours> BucketByProjectTask(Dictionary<string, TimeBooking> timebookings)
        {
            …
            dict[taskKeyGenerator.GetTaskKey(task)] = th;
            …
        }
    }
    

    Good for scenarios where thorough separation of concerns is required or where multiple complex key-building algorithms might exist, or even be provided from the “outside” by users of an API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data that looks like this . And my code below simply
I have a timer that loops through following code every second. this code reads
I have this javascript code below that uses jquery, it is suppoed to be
I have a code like this below, which gives me a $link that equals
I have this VBA below that is designed to loop through each WS and
I have python code below that will loop through a table and print out
I have this code below. I need to use a class id instead of
I have this code below. As you can see I am passing two variables
I have this code below: $insert = array(); for ($i = 1, $n =
I have this below code and it work fine header (content-type: text/xml); $xml =

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.