首页 > 解决方案 > 使用 Postgresql 在 EFCore 中按年龄过滤 - Npgsql

问题描述

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

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

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

标签: entity-framework-corenpgsqlef-core-2.2

解决方案


一个有效的解决方案:

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);
} 

推荐阅读