asp.net-mvc - 小时总数 (HH :MM) in Linq

标签 asp.net-mvc asp.net-core entity-framework-core ef-core-3.0

我的数据库表中有“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 evalutionEF 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/

相关文章:

c# - 使用 ControllerFeatureProvider 禁止特定 ASP.NET Core Controller 在不同主机中可用

entity-framework-core - 如何使用 Entity Framework Core 2 中的每层级表 (TPH) 模式将列持久保存为 json

c# - 使用 ASP.net Core Identity MVC 登录用户的登录 IP

c# - ASP.NET MVC - 登录成功,但 userId 返回 null

javascript - 如何: dynamic inline-block elements to start a new line when the container width is reach

c# - 在 ASP.NET MVC 中填充下拉列表

c# - 中间件网络核心命令

mysql - ASP.Net Core - EntityFrameworkCore 数据没有添加,而是更新

c# - EF Core 中是否有唯一约束的数据注释(代码优先)?

javascript - 如何让我的 TreeView 元素溢出超过父 div 的宽度?