尝试执行汇总查询以根据复杂的分组类型获得不同的计数。 “更清洁”的方式无法转换为SQL,“工作”的方式可转换为SQL。理想情况下,我希望采用“更清洁”的方法,因为它对我而言看起来更好。
为什么选择唯一计数在这里失败?
//我们要做的:(
var uniqueExercises = details
.GroupByWorkoutTypeThenWorkoutDateThenExerciseId(config.GroupByWorkoutType, config.TimePeriod)
.Select(x => new
{
Date = x.Key.Date,
WorkoutType = x.Key.WorkoutType,
ExerciseCount = 1
})
.GroupBy(x => new
{
x.Date,
x.WorkoutType
})
.Select(x => new
{
Date = x.Key.Date,
WorkoutType = x.Key.WorkoutType,
ExerciseCount = x.Sum(y => y.ExerciseCount)
});
//从逻辑上讲有意义但翻译不正确
var uniqueExercises = details
.GroupByWorkoutTypeThenWorkoutDate(config.GroupByWorkoutType, config.TimePeriod)
.Select(x => new
{
Date = x.Key.Date,
WorkoutType = x.Key.WorkoutType,
ExerciseCount = x.Select(x => x.Exercise.Id).Distinct().Count()
});