using System.Collections.Generic;
public class SafeProtectDeviceReportUsage
public long Vehicleid { get; set; }
public DateTime TimestampLastConnection { get; set; }
public double AvgConnectionDuration { get; set; }
public static void Main()
List<SafeProtectDeviceReportUsage> toInsertDatesRecords = new List<SafeProtectDeviceReportUsage>(){
new SafeProtectDeviceReportUsage(){TimestampLastConnection = new DateTime(2022,05,02),AvgConnectionDuration = 302,Vehicleid =1},
new SafeProtectDeviceReportUsage(){TimestampLastConnection = new DateTime(2022,05,02),AvgConnectionDuration = 1129,Vehicleid =1},
new SafeProtectDeviceReportUsage(){TimestampLastConnection = new DateTime(2022,05,02),AvgConnectionDuration = 500,Vehicleid =5},
new SafeProtectDeviceReportUsage(){TimestampLastConnection = new DateTime(2022,05,03),AvgConnectionDuration = 1440,Vehicleid =1}
var result = toInsertDatesRecords.GroupBy(row =>row.TimestampLastConnection.ToLocalTime().Date).Select(g => {
int vCount = g.Select(c => c.Vehicleid).Distinct().Count();
AvgConnectedDuration = g.Sum(x=> x.AvgConnectionDuration) / vCount
foreach(var item in result){
Console.WriteLine(item.date+ " "+ item.count+" "+item.AvgConnectedDuration);
Console.WriteLine("Hello World");