我可以在 Expression 主体内传递函数调用以进行 EF 6 查询 C# 7

问题描述 投票:0回答:1

我有以下代码。我尝试在查询的Where子句的表达式中传递对IsActive的调用,但它不执行

protected Expression<Func<vAccount, bool>> ActiveAccountPredicate => a =>
    (
        IsActive(a)
    )
    && (!a.Foreign || IsShowForeign)
    && (a.RegistrationTypeId != RegistrationType.NotRegistered || IsShowNotRegistered);

public static bool IsActive(vAccount a)
{
    return (a.DepartedDate == null || a.DepartedDate >= DateTime.Now)
            && (a.RegistrationTypeId != RegistrationType.NotRegistered || a.SupoDepartedDate == null || a.SupoDepartedDate >= DateTime.Now)
            && (a.RegistrationTypeId != RegistrationType.NotRegistered || a.RegistrationExpiredDate == null || a.RegistrationExpiredDate >= DateTime.Now);
}

有办法做到这一点吗?

c# entity-framework
1个回答
0
投票

您还需要将

IsActive
写为
Expression

public static Expression<Func<vAccount, bool>> IsActive => a =>
{
    return (a.DepartedDate == null || a.DepartedDate >= DateTime.Now)
            && (a.RegistrationTypeId != RegistrationType.NotRegistered || a.SupoDepartedDate == null || a.SupoDepartedDate >= DateTime.Now)
            && (a.RegistrationTypeId != RegistrationType.NotRegistered || a.RegistrationExpiredDate == null || a.RegistrationExpiredDate >= DateTime.Now);
};
© www.soinside.com 2019 - 2024. All rights reserved.