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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:51:21+00:00 2026-06-06T11:51:21+00:00

I need to sum the hours worked. I have 2 fields: startTime and endTime

  • 0

I need to sum the hours worked. I have 2 fields: startTime and endTime. Are “datetime” and “nullable“

I tried this but it returns error:

var timeWorked = res.AsEnumerable()
                    .Sum(f => f.endTime.HasValue ? f.endTime.Value : 0 - f.startTime .HasValue ? f.startTime .Value : 0);

Error: “Operator ‘-‘ cannot be applied to operands of type ‘bool’ and ‘int'”

The above code is in the following context

 try
 {                           
     using (serviciosDBDataContext miDataContext = new serviciosDBDataContext("Data Source='xxxxxxx'"))
      {
          var res = (from p in miDataContext.works
                     select new
                      {
                           p.idWork,
                           p.status,
                           p.amount,
                           p.startTime,
                           p.endTime    
                      }).ToList();                                 
//HOURS WORKED

    var timeWorked = res.AsEnumerable()
                        .Sum(f => f.endTime.HasValue ? f.endTime.Value : 0 - f.startTime .HasValue ? f.startTime .Value : 0);       
      }    
 }
 catch (Exception)
 {
        MessageBox.Show(AppResources.errCargaEstad);
 }
  • 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-06T11:51:23+00:00Added an answer on June 6, 2026 at 11:51 am

    You can use the GetValueOrDefault and Aggregate methods here:

    var timeWorked = res.AsEnumerable().Aggregate(TimeSpan.Zero, (total, f) => total + (f.endTime.GetValueOrDefault() - f.startTime.GetValueOrDefault());
    

    FYI in your example there’s a precedence issue, ie the following gets evaluated:

    f.endTime.HasValue ? f.endTime.Value : (0 - f.startTime.HasValue)
    

    Also, there is no implicit conversion between an int and a DateTime, so the conditional ternary operator cannot succeed.

    Update

    You can access the TotalMilliseconds, TotalSeconds… properties of TimeSpan to use these values in a method requiring a numeric type, however if you are dealing with time periods, the TimeSpan struct was created specifically for this purpose and mas reasoning about code far easier IMO.

    Update 2

    Okay, based on your comment of hours worked, I’d suggest renaming the variable from timeWorked to hoursWorked since an int representing a time without a unit given in the name can be misunderstood. The property the you require is TotalHours. This returns a double, you can explicitly cast to an int, however there may be a loss of precision:

    var hoursWorked = (int)res.Sum(f => (f.endTime.GetValueOrDefault() - f.startTime.GetValueOrDefault()).TotalHours);
    

    Also, since you are calling ToList to make res a list this forces the result into memory, so the call to AsEnumerable is unnecessary.

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

Sidebar

Related Questions

Have searched the database but need to specifically sum(of hours flown or days off)in
I need the sum of two database fields. I use this formula field :
I have the string SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10]) and I need to get the elements [A2:A10],[B2:B10],[C2:C10],[D2:D10] in
I have multiple tables that need to be merged into one. SELECT name, SUM(money)
I'm working on this website http://www.eluminatecreative.com/new/index.html and where it says sum'r fling I need
Jump to the EDIT for a better explanation! I have tried to get this
ok this is very simple yet have been searching for 3 hours and still
I have this MySql query: SELECT YEAR(date), MONTHNAME(date), SUM(block_time) FROM flights WHERE operator =
I need to calculate sum of hours based on period column and insert into
I have a query select user_id,sum(hours),date, task_id from table where used_id = 'x' and

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.