使用Postgresql在EFCore中按年龄过滤-Npgsql

时间:2019-05-24 21:33:24

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

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

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

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

1 个答案:

答案 0 :(得分:0)

有效的解决方案:

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