如何使用 EF Core 按周 对数据分组?

咨询区

  • Aza

在 Entity Framework 6 中,我可以用 SqlFunctions.DatePart() 函数来实现,参考如下代码:

var byWeek = data.GroupBy(x => SqlFunctions.DatePart("week", x.Date));

现在的问题是, Entity Framework Core 中不提供如 DbFunctionsSqlFunctions 方法,这就很尴尬了,请问我该如何解决呢?

回答区

  • cyptus

既然没有现成的函数,可以自己实现一个,将 sql 中的 datepart 方法用 C# 中的 DbFunctionAttribute 给标注上,这样做的好处是告诉 efcore 不要去以 string 方式处理 detepart 参数,参考代码如下:

DbContext:

public int? DatePart(string datePartArg, DateTime? date) => throw new Exception();

public void OnModelCreating(DbModelBuilder modelBuilder)
{
    var methodInfo = typeof(DbContext).GetRuntimeMethod(nameof(DatePart), new[] { typeof(string), typeof(DateTime) });
    modelBuilder.HasDbFunction(methodInfo)
                .HasTranslation(args => new SqlFunctionExpression(nameof(DatePart), typeof(int?), new[]
                {
                        new SqlFragmentExpression(args.ToArray()[0].ToString()), args.ToArray()[1]
                }));
}

查询方式:

repository.GroupBy(x => dbContext.DatePart("week", x.CreatedAt));

更多信息参考 github:https://github.com/aspnet/EntityFrameworkCore/issues/10404

  • Nikolay Kostov

确实有些麻烦,我现在的变通方法是除7实现,首先自定义个方法,代码如下:

private DateTime GetFirstMondayOfYear(int year)
{
    var dt = new DateTime(year, 1, 1);
    while (dt.DayOfWeek != DayOfWeek.Monday)
    {
        dt = dt.AddDays(1);
    }

    return dt;
}

然后在分组的时候计算。

var firstMondayOfYear = this.GetFirstMondayOfYear(DateTime.Now.Year);

var entries = this.entitiesService.FindForLastMonths(this.CurrentUser.Id, 6)
                        .GroupBy(x => ((int)(x.Date - firstMondayOfYear).TotalDays / 7))

这个分组会得到当前年的周个数,负值是表示挂在前一年的, 之后就可以通过下面的属性方法得到当前的周名称。

public string WeekName
{
    get
    {
        var year = DateTime.Now.AddYears((int)Math.Floor(this.WeekNumber / 52.0)).Year;
        var weekNumber = this.WeekNumber % 52;
        
        while (weekNumber < 0)
        {
            weekNumber += 52;
        }

        return $"{year}, W{weekNumber}";
    }
}

点评区

sql处理稍微复杂一点,用ef处理起来还是有些麻烦的,没啥好说的,学习了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Entity Framework CoreEF Core)进行数据库操作时,你不需要显式打开或关闭数据库连接。EF Core 会自动管理数据库连接的打开和关闭。 要使用 SQL 更新数据,你可以使用 EF Core 提供的 `ExecuteSqlRaw` 或 `ExecuteSqlInterpolated` 方法。这些方法允许你执行原始的 SQL 查询或命令。 下面是一个示例代码,展示了如何使用 EF Core 执行原始 SQL 更新数据: ```csharp using Microsoft.EntityFrameworkCore; using System; // 创建 DbContext 类 public class YourDbContext : DbContext { public YourDbContext(DbContextOptions<YourDbContext> options) : base(options) { } // DbSet 和其他属性... public DbSet<IPS_Invoice> IPS_Invoices { get; set; } } public class IPS_Invoice { public int IPS_ID { get; set; } public bool BLOCK { get; set; } public DateTime? BLOCKTIME { get; set; } } public class YourRepository { private readonly YourDbContext _dbContext; public YourRepository(YourDbContext dbContext) { _dbContext = dbContext; } public string UpdateBlockTime(IPS_Invoice model) { string message = ""; try { int affectedRows = _dbContext.Database.ExecuteSqlInterpolated($"UPDATE IPS_Invoices SET BLOCK = true, BLOCKTIME = {DateTime.Now} WHERE IPS_ID = {model.IPS_ID}"); if (affectedRows > 0) { message = "True"; } else { message = "No records updated"; } } catch (Exception e) { message = "False"; // 处理异常... } return message; } } ``` 在上述代码中,我们首先创建了一个继承自 `DbContext` 的 `YourDbContext` 类,并定义了需要操作的实体类 `IPS_Invoice`。然后,我们创建了一个名为 `YourRepository` 的仓储类,在该类中使用 `ExecuteSqlInterpolated` 方法执行原始的 SQL 更新操作。 在 `UpdateBlockTime` 方法中,我们使用插值字符串(interpolated string)来构建 SQL 命令,并将其传递给 `ExecuteSqlInterpolated` 方法进行执行。如果更新操作成功影响了一行或多行数据,返回的 `affectedRows` 将大于 0。 请根据你的实际情况修改代码,并确保在使用 EF Core 时按照最佳实践进行数据库操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值