使用 .Net 6 和 VS2022 ,考虑以下代码:
DateOnly dateOnly= new DateOnly(2022,12,24);
DateTime dateTime = DateTime.Now;
if (dateTime > dateOnly)
{
}
Run Code Online (Sandbox Code Playgroud)
这会导致这个错误:
Operator '>' cannot be applied to operands of type 'DateTime' and 'DateOnly'
即使没有内置属性可以DateOnly从 a 中获取DateTime,而无需编写一些自定义扩展方法,这些DateOnly.Compare方法也不支持与DateTime类型进行比较。这个故事是一样的TimeOnly如果我没有遗漏什么,比较这两种类型的正确方法是什么?
更新:
刚刚发现甚至不可能像 webapi 查询参数中的其他类型一样使用这些类型!此外,EF core 6 在 SqlClient 中没有对这些类型的内置支持!
也许延迟使用这些类型会更好......