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);
}
You can use the GetValueOrDefault and Aggregate methods here:
FYI in your example there’s a precedence issue, ie the following gets evaluated:
Also, there is no implicit conversion between an
intand aDateTime, 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
timeWorkedtohoursWorkedsince an int representing a time without a unit given in the name can be misunderstood. The property the you require is TotalHours. This returns adouble, you can explicitly cast to anint, however there may be a loss of precision:Also, since you are calling
ToListto makeresa list this forces the result into memory, so the call toAsEnumerableis unnecessary.