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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:47:35+00:00 2026-06-17T16:47:35+00:00

I need a list with some objects for calculation. my current code looks like

  • 0

I need a list with some objects for calculation.

my current code looks like this

private class HelperClass
{
    public DateTime TheDate {get;set;}
    public TimeSpan TheDuration {get;set;}
    public bool Enabled {get;set;}
}

private TimeSpan TheMethod()
{
    // create entries for every date
    var items = new List<HelperClass>();
    foreach(DateTime d in GetAllDatesOrdered())
    {
        items.Add(new HelperClass { TheDate = d, Enabled = GetEnabled(d), });
    }

    // calculate the duration for every entry
    for (int i = 0; i < items.Count; i++)
    {
        var item = items[i];
        if (i == items.Count -1) // the last one
            item.TheDuration = DateTime.Now - item.TheDate;
        else
            item.TheDuration = items[i+1].TheDate - item.TheDate;
    }

    // calculate the total duration and return the result
    var result = TimeSpan.Zero;
    foreach(var item in items.Where(x => x.Enabled))
        result = result.Add(item.TheDuration);
    return result;
}

Now I find it a bit ugly just to introduce a type for my calculation (HelperClass).
My first approach was to use Tuple<DateTime, TimeSpan, bool> like I usually do this but since I need to modify the TimeSpan after creating the instance I can’t use Tuple since Tuple.ItemX is readonly.

I thought about an anonymous type, but I can’t figure out how to init my List

var item1 = new { TheDate = DateTime.Now,
                  TheDuration = TimeSpan.Zero, Enabled = true };

var items = new List<?>(); // How to declare this ???
items.Add(item1);
  • 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-17T16:47:36+00:00Added an answer on June 17, 2026 at 4:47 pm

    Using a projection looks like the way forward to me – but you can compute the durations as you go, by “zipping” your collection with itself, offset by one. You can then do the whole method in one query:

    // Materialize the result to avoid computing possibly different sequences
    var allDatesAndNow = GetDatesOrdered().Concat(new[] { DateTime.Now })
                                          .ToList();
    
    return allDatesNow.Zip(allDatesNow.Skip(1),
                           (x, y) => new { Enabled = GetEnabled(x),
                                           Duration = y - x })
                      .Where(x => x.Enabled)
                      .Aggregate(TimeSpan.Zero, (t, pair) => t + pair.Duration);
    

    The Zip call pairs up each date with its subsequent one, converting each pair of values into a duration and an enabled flag. The Where call filters out disabled pairs. The Aggregate call sums the durations from the resulting pairs.

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

Sidebar

Related Questions

In a simplified situation like this one, with 3 classes: class ClassONE{ protected List<IntermediatedClass>;
I need to test method which returns ordered List of some complex objects. Simplified
I'm creating a simple file walker to list some files and need to omit
I need to create one drop down list contains some Languages. By selecting any
Hi Programmers! Actually i need the distinct values from the List after adding some
I have some text i need to filter out a list of bad words
I need to move some selected items from a list by clicking on a
I need to do some special operation for the last element in a list.
I have list of catalog paths and need to filter out some of them.
In my application I need to retrieve the list of venues and exclude some

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.