【问题标题】:Filter by Age in EFCore using Postgresql - Npgsql使用 Postgresql 在 EFCore 中按年龄过滤 - Npgsql
【发布时间】:2019-05-25 09:14:39
【问题描述】:

是否可以将以下 Postgresql 查询转换为 EFCore?

SELECT "applic"."age" FROM (
   SELECT EXTRACT(YEAR FROM age(birthdate)) :: int AS "age" FROM public.applicant
) AS "applic"
WHERE "applic"."age" < 50;

我查看了文档,但找不到任何有用的信息。

【问题讨论】:

  • 一个好的开始是尝试一些东西并向我们展示你做了什么以及它在哪里没有成功(如果没有)。只有这样我们才能看到具体您需要帮助的地方。
  • @GertArnold 我发布了一个可行的解决方案。

标签: entity-framework-core npgsql ef-core-2.2


【解决方案1】:

有效的解决方案:

var applicants = from s in this.RepositoryContext.Applicants select s;

if (query.AgeStart != null && query.AgeEnd != null)
{
    applicants = applicants.Where(c => (DateTime.Today.Year - c.BirthDate.Year) >= query.AgeStart && (DateTime.Today.Year - c.BirthDate.Year) < query.AgeEnd);
} 

【讨论】:

    猜你喜欢
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多