C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,419 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have a table:
20.2.2021 12:54:34
20.2.2021 13:54:34
20.2.2021 14:54:34
20.2.2021 15:54:34
20.2.2021 16:54:34
21.2.2021 11:50:00
21.2.2021 13:54:34
21.2.2021 14:54:34
22.2.2021 10:00:00
22.2.2021 13:54:34
22.2.2021 14:54:34
I need to pick up 20.2.2021 12:54:34 & 21.2.2021 11:50:00 & 22.2.2021 10:00:00 and calculate average time.
Here is my current code:
List<TimeSpan> result = db.Log
.Where(w => w.DateToday.Date >= startDate.Date && w.DateToday.Date <= endDate.Date)
.Select(a => a.DateToday.TimeOfDay)
.ToList();
double doubleAverageTicks = result.Average(timeSpan => timeSpan.Ticks);
long longAverageTicks = Convert.ToInt64(doubleAverageTicks);
return new TimeSpan(longAverageTicks);
It is capable to calculate average, but it is currently taking all dates and calculating average. I need to select only first (Min) time record each day. How to achieve that?
To get the dates, try a query like this:
IEnumerable<DateTime> days_min_time =
db.Log
.Select( d => d.DateToday )
.GroupBy( g => g.Date )
.Select( g => g.Min( ) );
Then perform other operations.