在 Entity Framework Core LINQ 查询 return 中将字符串转换为 DateTime 有何作用?

What does casting a string to DateTime in an Entity Framework Core LINQ query return?

我的应用程序使用 Entity Framework Core 从 SQL 数据库加载数据。为了避免加载我的 table Schedule 的所有数据条目并在之后过滤它们,我想通过将 Schedule.Date 条目(一个字符串)与一个字符串进行比较来在数据库查询中过滤它们先前创建的 DateTime 个名为 targetDate 的对象。由于无法在此查询中直接使用 DateTime.ParseSchedule.Date 转换为 DateTime,因此我将其转换为 object,然后显式转换为 DateTime .下面这行代表我在做什么:

Schedule schedule = _context.Schedule.Where(s => (DateTime)(object)s.Date >= targetDate).First();

目前效果很好。但是,我不想 运行 以后出现问题,所以我需要了解在这种情况下转换为 DateTime 使用的格式。虽然数据库中的日期是字符串,但它们都是以 en-US 格式提供的,并且转换它们似乎只使用这种格式,这会很棒。我如何确定它始终使用这种格式以及在使用这样的转换时如何确定要使用的格式?

假设您正在使用 SQL 服务器

Though the dates in the database are strings,

出于多种原因,这很糟糕。此处您需要 table 扫描以将所有字符串转换为日期以执行比较。

they are all provided in the format en-US and casting them appears to use just this format, which would be great. How can I be sure that it always uses this format and how is the format to be used determined when using a cast like this?

转换在 SQL 服务器中完成,该服务器将根据当前 language 连接将字符串转换为日期。