【问题标题】:Entity Framework Core NodaTime grouping by dateEntity Framework Core NodaTime 按日期分组
【发布时间】:2021-03-08 02:36:27
【问题描述】:

我正在使用 NodaTime 和 Entity Framework Core,现在我正在尝试按日期对查询进行分组,尽管我使用 Instant 作为字段数据类型。据我了解,使用 dateColumn.Date(使用 DateTime 时)应该可以工作并将查询转移到 date_trunc('day', dateColumn),但是如何使用 NodaTime Instant 类型实现呢?

我看到的问题是 Instant 需要通过时区才能解决这一天,这会抛出

要么以可翻译的形式重写查询...

【问题讨论】:

    标签: entity-framework-core npgsql nodatime


    【解决方案1】:

    如果您需要使用人类日历单位(例如天、月...)执行操作,那么请考虑改用 LocalDateTime。 Instant 的想法正是它不支持人类日历操作等。

    这样的事情应该可以工作:

    _ = await ctx.Blogs.GroupBy(b => b.Creation.Date).CountAsync();
    
    public class BlogContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.UseNpgsql(@"...", o => o.UseNodaTime());
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public LocalDateTime Creation { get; set; }
    }
    

    【讨论】:

    • 这就是我真正期望的解决方案 - 你有一个例子我将如何使用 LocalDateTime 字段执行上述请求?
    猜你喜欢
    • 1970-01-01
    • 2017-07-29
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多