Linq 的总小时数 (HH:MM)

Aja*_*Aju 3 asp.net-mvc entity-framework-core asp.net-core ef-core-3.0

我的数据库表中有 HH.mm 格式的“TimeClocked”,我想使用 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();
Run Code Online (Sandbox Code Playgroud)

我正在使用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.
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。

Ale*_*der 5

这是由于受限制的客户端评估而发生的,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();
Run Code Online (Sandbox Code Playgroud)