Linq的小时总和(HH:MM) [英] Sum of hours (HH:MM) in Linq

查看:101
本文介绍了Linq的小时总和(HH:MM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库表中有"TimeClocked",格式为HH.mm,我想使用LINQ对所有"TimeClocked"求和.

I am having "TimeClocked" in my database table in the format HH.mm and I want to sum all the "TimeClocked" using LINQ.

我已经尝试过此聚合函数.

I have tried this aggregate function.

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 .它显示出这样的错误.

I am using EF Core 3.0.0. It's showing error like this.

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.

任何帮助将不胜感激.

推荐答案

由于

It happens due to restricted client evalution in EF Core 3 so you have to call AsEnumerable right before untranslatable expressions

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();

这篇关于Linq的小时总和(HH:MM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆