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 8629123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:43:19+00:00 2026-06-12T08:43:19+00:00

Say I have a class like so: public class Work { public string Name;

  • 0

Say I have a class like so:

public class Work
{
    public string Name;
    public double Time;

    public Work(string name, double time)
    {
        Name = name;
        Time = time;
    }
 }

And I have a List<Work> with about 20 values that are all filled in:

List<Work> workToDo = new List<Work>();
// Populate workToDo

Is there any possible way that I can group workToDo into segments where each segments sum of Time is a particular value? Say workToDo has values like so:

Name | Time
A    | 3.50
B    | 2.75
C    | 4.25
D    | 2.50
E    | 5.25
F    | 3.75

If I want the sum of times to be 7, each segment or List<Work> should have a bunch of values where the sum of all the Times is 7 or close to it. Is this even remotely possible or is it just a stupid question/idea? I am using this code to separate workToDo into segments of 4:

var query = workToDo.Select(x => x.Time)
        .Select((x, i) => new { Index = i, Value = x})
        .GroupBy(y => y.Index / 4)
        .ToList();

But I am not sure how to do it based on the Times.

  • 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-12T08:43:20+00:00Added an answer on June 12, 2026 at 8:43 am

    This solution recurses through all combinations and returns the ones whose sums are close enough to the target sum.

    Here is the pretty front-end method that lets you specify the list of work, the target sum, and how close the sums must be:

    public List<List<Work>> GetCombinations(List<Work> workList,
                                            double targetSum,
                                            double threshhold)
    {
        return GetCombinations(0,
                               new List<Work>(),
                               workList,
                               targetSum - threshhold,
                               targetSum + threshhold);
    }
    

    Here is the recursive method that does all of the work:

    private List<List<Work>> GetCombinations(double currentSum,
                                             List<Work> currentWorks,
                                             List<Work> remainingWorks,
                                             double minSum,
                                             double maxSum)
    {
        // Filter out the works that would go over the maxSum.
        var newRemainingWorks = remainingWorks.Where(x => currentSum + x.Time <= maxSum)
                                              .ToList();
        // Create the possible combinations by adding each newRemainingWork to the 
        // list of current works.
        var sums = newRemainingWorks
                       .Select(x => new
                              {
                                  Works = currentWorks.Concat(new [] { x }).ToList(),
                                  Sum = currentSum + x.Time
                              })                                
                       .ToList();
        // The initial combinations are the possible combinations that are
        // within the sum range.                   
        var combinations = sums.Where(x => x.Sum >= minSum).Select(x => x.Works);
        // The additional combinations get determined in the recursive call.
        var newCombinations = from index in Enumerable.Range(0, sums.Count)
                              from combo in GetCombinations
                                            (
                                                sums[index].Sum,
                                                sums[index].Works,
                                                newRemainingWorks.Skip(index + 1).ToList(),
                                                minSum,
                                                maxSum
                                            )
                              select combo;
        return combinations.Concat(newCombinations).ToList();        
    }
    

    This line will get combinations that sum to 7 +/- 1:

    GetCombinations(workToDo, 7, 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a structure like: class SomeObject Public Name as String Public Created
say, I have a partial class that contains a custom property like this public
Say I have a class that looks like this (get/set omited): class InfoClass{ String
If I have entities like: @Entity public class Person { public String name; @OneToMany
Say I have a .NET class like so: public class Person { public string
Say I have this class: @Entity @Table(name=PICTURE) public class Picture{ private String category1, category2;
Say I have a simple address class like below: public class Address { public
lets say I have my owndefined datatype like this: public class InformationType { private
Say I have a class with three constructors, like the one below: public class
Say I have a couple of basic objects like so: [Serializable] public class Base

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.