entity-framework-core - Entity Framework Core NodaTime 按日期分组
问题描述
我正在使用 NodaTime 和 Entity Framework Core,现在我正在尝试按日期对查询进行分组,尽管我使用Instant
的是字段数据类型。据我了解,使用 dateColumn.Date (使用 DateTime 时)应该可以工作并将查询转移到date_trunc('day', dateColumn)
,但是如何使用 NodaTimeInstant
类型来实现呢?
我看到的问题是 Instant 需要通过时区才能解决这一天,这会抛出
要么以可以翻译的形式重写查询......
解决方案
If you need to perform operations with human calendar units (e.g. days, months...), then considering using LocalDateTime instead. The idea of Instant is precisely that it doesn't support human calendar operations etc.
Something like this should work:
_ = 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; }
}
推荐阅读
- vb.net - 将 vb.net 中的 post 请求传递给 API 控制器时,请求对象显示空值
- java - 在JAVA中获取Wi-Fi名称和接口
- javascript - 将网页图片替换为另一个保持相同文件名的图片
- bash - Split export format in bash
- php - 如何使用 LINE bot 连接到 mysql 数据库?
- javascript - Selenium (Java) 等到加载 JavaScript / Vue / Angular
- c# - 我可以根据抽象基类中定义的某个属性创建派生类的实例吗?
- xcode - Xcode10 - xcworkspace 文件仅运行我的应用程序的旧版本
- sql-server - SQL Server:日期列中的月份名称
- clojure - with cloure lein ring server, how to set max heapsize when starting the web appication?