LINQ 查询 NotSupportedException OrderBy Average of TimeSpan
LINQ query NotSupportedException OrderBy Average of TimeSpan
var seasonPlayer = (from SeasonPlayer in db.SeasonPlayerSet
orderby SeasonPlayer.StatisticsPlayer.Average(x => x.STP_timeplay.Ticks) descending
select SeasonPlayer).ToList();
SeasonPlayer 有一个 StatisticsPlayer 的 ICollection,所以我想获得按 STP_timeplay 降序排列的平均花在球场上的时间,这是 TimeSpan 的一种类型。我无法通过 STP_timeplay 获得平均值,因为它不是小数,所以我尝试通过 Ticks 获得平均值。它抛出异常:
The specified type member 'Ticks' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
试试这个:-
var seasonPlayer = db.SeasonPlayerSet.ToList()
.OrderByDescending(x => x.StatisticsPlayer
.Average(z => z.STP_timeplay.Ticks);
问题是 Linq to Entities 查询提供程序无法将您的 LINQ 转换为 Sql 查询,该查询连接到 Statistics Player,平均时间,按赛季球员分组。
鉴于您似乎在迭代所有赛季球员,如果记录的数量不是太多,您可以像这样将它们全部存入内存:
var seasonPlayer = db.SeasonPlayerSet
.Include(sp => sp.StatisticsPlayer)
.ToList()
.Select(sp => new {SeasonPlayer = sp, Average = sp.StatisticsPlayer.Average(stp => stp.STP_timeplay.Ticks)})
.OrderByDescending(sp => sp.Average)
.Select(sp => SeasonPlayer)
.ToList();
var seasonPlayer = (from SeasonPlayer in db.SeasonPlayerSet
orderby SeasonPlayer.StatisticsPlayer.Average(x => x.STP_timeplay.Ticks) descending
select SeasonPlayer).ToList();
SeasonPlayer 有一个 StatisticsPlayer 的 ICollection,所以我想获得按 STP_timeplay 降序排列的平均花在球场上的时间,这是 TimeSpan 的一种类型。我无法通过 STP_timeplay 获得平均值,因为它不是小数,所以我尝试通过 Ticks 获得平均值。它抛出异常:
The specified type member 'Ticks' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
试试这个:-
var seasonPlayer = db.SeasonPlayerSet.ToList()
.OrderByDescending(x => x.StatisticsPlayer
.Average(z => z.STP_timeplay.Ticks);
问题是 Linq to Entities 查询提供程序无法将您的 LINQ 转换为 Sql 查询,该查询连接到 Statistics Player,平均时间,按赛季球员分组。
鉴于您似乎在迭代所有赛季球员,如果记录的数量不是太多,您可以像这样将它们全部存入内存:
var seasonPlayer = db.SeasonPlayerSet
.Include(sp => sp.StatisticsPlayer)
.ToList()
.Select(sp => new {SeasonPlayer = sp, Average = sp.StatisticsPlayer.Average(stp => stp.STP_timeplay.Ticks)})
.OrderByDescending(sp => sp.Average)
.Select(sp => SeasonPlayer)
.ToList();