多重平均数和总和
Multiple Averages and sum
我在列表 (toInsertDatesRecords) 中有以下对象。
class SafeProtectDeviceReportUsage
{
public long Vehicleid { get; set; }
public DateTime TimestampLastConnection { get; set; }
// Minutes
public double AvgConnectionDuration { get; set; }
}
我需要 return 所有车辆在特定日期 (dd/MM) 的平均 AvgConnectionDuration。
这是一个数据样本,同一辆车的 AvgConnectedDuration 有许多值。
Date
VehicleId
AvgConnectedDuration
05/02
1
302
05/02
1
1129
05/02
5
500
05/03
1
1440
预期结果是
Date
count
duration
05/02
2
302 + 1129 + 500 / 2
05/03
1
1440 / 1
我提出了以下查询:
var toBeReturned = toInsertDatesRecords.GroupBy(row => row.TimestampLastConnection.ToLocalTime().Date)
.Select(g =>
{
int vCount = g.Select(c => c.Vehicleid).Distinct().Count();
return new
{
date = g.Key.Date,
count = vCount,
duration = g.Average(c => c.AvgConnectionDuration)
};
});
这是错误的,因为在计算所有车辆的平均持续时间之前,我需要先对每辆车在特定日期的 AvgConnectionDuration 求和。
我不知道如何按两次分组。
尝试以下操作:
var toBeReturned = toInsertDatesRecords.GroupBy(row => row.TimestampLastConnection.ToLocalTime().Date)
.Select(g => new {
date = g.Key.Date,
count = g.Select(c => c.Vehicleid).Distinct().Count(),
duration = g.Average(c => c.AvgConnectionDuration)
}
).select(g => new {date = g.date, count = g.Sum(h => h.count), duration = g,Average(h => h.count));
根据您的示例数据和预期结果,您可以尝试使用 Sum
并除以计数而不是使用 Average
var result = toInsertDatesRecords
.GroupBy(row =>row.TimestampLastConnection.ToLocalTime().Date)
.Select(g =>
{
int vCount = g.Select(c => c.Vehicleid).Distinct().Count();
return new
{
date = g.Key.Date,
count = vCount,
AvgConnectedDuration = g.Sum(x=> x.AvgConnectionDuration) / vCount
};
});
而且我认为我们可以使用 g.GroupBy(x=>x.Vehicleid).Count()
计算数字使其变得简单。
var result = toInsertDatesRecords
.GroupBy(row =>row.TimestampLastConnection.ToLocalTime().Date)
.Select(g =>
{
int vCount = g.GroupBy(x=>x.Vehicleid).Count();
return new
{
date = g.Key.Date,
count = vCount,
AvgConnectedDuration = g.Sum(x=> x.AvgConnectionDuration) / vCount
};
});
我在列表 (toInsertDatesRecords) 中有以下对象。
class SafeProtectDeviceReportUsage
{
public long Vehicleid { get; set; }
public DateTime TimestampLastConnection { get; set; }
// Minutes
public double AvgConnectionDuration { get; set; }
}
我需要 return 所有车辆在特定日期 (dd/MM) 的平均 AvgConnectionDuration。
这是一个数据样本,同一辆车的 AvgConnectedDuration 有许多值。
Date | VehicleId | AvgConnectedDuration |
---|---|---|
05/02 | 1 | 302 |
05/02 | 1 | 1129 |
05/02 | 5 | 500 |
05/03 | 1 | 1440 |
预期结果是
Date | count | duration |
---|---|---|
05/02 | 2 | 302 + 1129 + 500 / 2 |
05/03 | 1 | 1440 / 1 |
我提出了以下查询:
var toBeReturned = toInsertDatesRecords.GroupBy(row => row.TimestampLastConnection.ToLocalTime().Date)
.Select(g =>
{
int vCount = g.Select(c => c.Vehicleid).Distinct().Count();
return new
{
date = g.Key.Date,
count = vCount,
duration = g.Average(c => c.AvgConnectionDuration)
};
});
这是错误的,因为在计算所有车辆的平均持续时间之前,我需要先对每辆车在特定日期的 AvgConnectionDuration 求和。
我不知道如何按两次分组。
尝试以下操作:
var toBeReturned = toInsertDatesRecords.GroupBy(row => row.TimestampLastConnection.ToLocalTime().Date)
.Select(g => new {
date = g.Key.Date,
count = g.Select(c => c.Vehicleid).Distinct().Count(),
duration = g.Average(c => c.AvgConnectionDuration)
}
).select(g => new {date = g.date, count = g.Sum(h => h.count), duration = g,Average(h => h.count));
根据您的示例数据和预期结果,您可以尝试使用 Sum
并除以计数而不是使用 Average
var result = toInsertDatesRecords
.GroupBy(row =>row.TimestampLastConnection.ToLocalTime().Date)
.Select(g =>
{
int vCount = g.Select(c => c.Vehicleid).Distinct().Count();
return new
{
date = g.Key.Date,
count = vCount,
AvgConnectedDuration = g.Sum(x=> x.AvgConnectionDuration) / vCount
};
});
而且我认为我们可以使用 g.GroupBy(x=>x.Vehicleid).Count()
计算数字使其变得简单。
var result = toInsertDatesRecords
.GroupBy(row =>row.TimestampLastConnection.ToLocalTime().Date)
.Select(g =>
{
int vCount = g.GroupBy(x=>x.Vehicleid).Count();
return new
{
date = g.Key.Date,
count = vCount,
AvgConnectedDuration = g.Sum(x=> x.AvgConnectionDuration) / vCount
};
});