有点晚了,但有可能(如上所述发布示例代码),我今天遇到了完全相同的情况(构造函数重载,因此无法更改名称)。这是我的做法,小技巧,但它让我将所有相关的 LINQ 谓词放在同一个地方:
public BusinessStructureFilterSpecification(int responsibilityTypeId, bool dummy1 = true) : base(x => x.ResponsibleGroups.Any(x1 => x1.ResponsibilityTypeId == responsibilityTypeId))
{
AddIncludes();
}
public BusinessStructureFilterSpecification(int userId, string dummy2 = "") : base(x => x.ResponsibleUsers.Any(x1 => x1.UserId == userId))
{
AddIncludes();
}
现在的诀窍是使用参数名称来调用它们,如下所示:
if (responsibiltyTypeId.HasValue && !userId.HasValue)
spec = new BusinessStructureFilterSpecification(responsibilityTypeId: responsibiltyTypeId.Value);
if (!responsibiltyTypeId.HasValue && userId.HasValue)
spec = new BusinessStructureFilterSpecification(userId: userId.Value);