我的数据库表中有“TimeClocked”,格式为 HH.mm,我想使用 LINQ 对所有“TimeClocked”求和。
我尝试过这个聚合函数。
var data = _projectDbContext
.Tasks
.Where(x => x.Project.CompanyId == companyId && x.Status == true)
.Select(e => TimeSpan.Parse(e.TimeClocked))
.Aggregate(TimeSpan.FromMinutes(0), (total, next) => total + next)
.ToString();
我正在使用EF Core 3.0.0。它显示这样的错误。
Processing of the LINQ expression 'Aggregate<TimeSpan, TimeSpan>(
source: Select<TaskEntity, TimeSpan>(
source: Where<TaskEntity>(
source: DbSet<TaskEntity>,
predicate: (x) => x.Project.CompanyId == (Unhandled parameter: __companyId_0) && x.Status == True),
selector: (e) => Parse(e.TimeClocked)),
seed: (Unhandled parameter: __p_1),
func: (total, next) => total + next)' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.
如有任何帮助,我们将不胜感激。
最佳答案
发生这种情况是由于 restricted client evalution在 EF Core 3
中,因此您必须在不可翻译的表达式之前调用 AsEnumerable
var data = _projectDbContext
.Tasks
.Where(x => x.Project.CompanyId == companyId && x.Status == true)
.AsEnumerable() // switches to LINQ to Objects
.Select(e => TimeSpan.Parse(e.TimeClocked))
.Aggregate(TimeSpan.FromMinutes(0), (total, next) => total + next)
.ToString();
关于asp.net-mvc - 小时总数 (HH :MM) in Linq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58286139/