C#如何缩短多个If表达式

Jac*_*kal 8 c# if-statement asp.net-core

我有多个验证布尔属性,可以需要也可以不需要。如果需要它们,则需要对其进行检查以进行验证。因此我不得不构建多个 if 语句来处理每个属性。我的问题是是否有更好的方法来维护这一点,而不必为每个新属性编写 if。

public class ProductionNavbarViewModel
{
    public bool IsProductionActive { get; set; }
    public bool ValidatedComponents { get; set; }
    public bool ValidatedGeometries { get; set; }
    public bool ValidatedPokayokes { get; set; }
    public bool ValidatedTechnicalFile { get; set; }
    public bool ValidatedStandardOperationSheet { get; set; }
    public bool ValidatedOperationMethod { get; set; }
    public bool IsComponentsRequired { get; set; }
    public bool IsGeometriesRequired { get; set; }
    public bool IsPokayokesRequired { get; set; }
    public bool IsTechnicalFileRequired { get; set; }
    public bool IsStandardOperationSheetRequired { get; set; }
    public bool IsOperationMethodRequired { get; set; }


    public bool IsProductionReadyToStart()
    {
        if (IsComponentsRequired)
        {
            return ValidatedComponents;
        }

        if (IsComponentsRequired && IsGeometriesRequired)
        {
            return ValidatedComponents && ValidatedGeometries;
        }

        if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired)
        {
            return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes;
        }

        if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired && IsTechnicalFileRequired)
        {
            return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes && ValidatedTechnicalFile;
        }

        if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired && IsTechnicalFileRequired && ValidatedStandardOperationSheet)
        {
            return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes && ValidatedTechnicalFile && ValidatedStandardOperationSheet;
        }

        if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired && IsTechnicalFileRequired && IsStandardOperationSheetRequired && IsOperationMethodRequired)
        {
            return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes && ValidatedTechnicalFile && ValidatedStandardOperationSheet && ValidatedOperationMethod;
        }

        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

制作此代码时出现问题。目的是验证所有选项,它们必须是必要的,如果只有一个属性满足条件,则无法返回。

谢谢大家,我会在评论中尝试一些建议的方法,然后我会发布结果

更新

我现在有一个简短的版本,根据每个人的评论更具可读性,直到我可以尝试每一种方法。根据@Alexander Powolozki 的答案编辑以组合所有表达式。

    public bool IsProductionReadyToStart()
    {
        bool isValid = true;

        isValid &= !IsComponentsRequired || ValidatedComponents;
        isValid &= !IsGeometriesRequired || ValidatedGeometries;
        isValid &= !IsPokayokesRequired || ValidatedComponents;
        isValid &= !IsTechnicalFileRequired || ValidatedTechnicalFile;
        isValid &= !IsStandardOperationSheetRequired || ValidatedStandardOperationSheet;
        isValid &= !IsOperationMethodRequired || ValidatedOperationMethod;            

        return isValid;
    }
Run Code Online (Sandbox Code Playgroud)

Ale*_*zki 7

该方法的正确实现应该如下所示:

public bool IsProductionReadyToStart()
{
    bool isValid = true;

    isValid &= !IsComponentsRequired || ValidatedComponents;
    isValid &= !IsGeometriesRequired || ValidatedGeometries;
    isValid &= !IsPokayokesRequired || ValidatedPokayokes;
    isValid &= !IsTechnicalFileRequired || ValidatedTechnicalFile;
    isValid &= !IsStandardOperationSheetRequired || ValidatedStandardOperationSheet;
    isValid &= !IsOperationMethodRequired || ValidatedOperationMethod;            

    return isValid;
}
Run Code Online (Sandbox Code Playgroud)

当不使用 &= 时,您将删除您检查过的所有先前结果,而不是将它们合并。


Fab*_*bio 7

它看起来像一个集合

public class Validation
{
    public bool Required { get; set; }
    public bool IsValid { get; set; }
}

var validations = new[]
{
    new Validation { Required = true, IsValid = true },
    new Validation { Required = false, IsValid = true },
    new Validation { Required = true, IsValid = false },
};

// return true only when all required validations are valid
public bool IsProductionReadyToStart()
{
    return _validations.Where(v => v.Required).All(v => v.IsValid);
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,这看起来是最好的方法,我之前考虑过它,但它需要对我的模型进行一些更改并创建一个模型来添加验证 (2认同)

C.E*_*uis 5

我会去:

if (IsComponentsRequired && !ValidateComponents) return false;
if (IsGeometriesRequired && !ValidatedGeometries) return false;
...
return true;
Run Code Online (Sandbox Code Playgroud)

这更像是一个清单。


Pav*_*ski 5

您可以将您的条件累积到ValueTuple集合中,然后一起检查

var conditions = new[]
{
    (IsComponentsRequired, ValidatedComponents),
    (IsGeometriesRequired, ValidatedGeometries),
    (IsPokayokesRequired, ValidatedPokayokes)
};

return conditions.Where(c => c.Item1).All(c => c.Item2);
Run Code Online (Sandbox Code Playgroud)

您还可以使用命名元组语法来提高可读性

var conditions = new (bool isRequired, bool validated)[]
{
    (IsComponentsRequired, ValidatedComponents),
    (IsGeometriesRequired, ValidatedGeometries),
    (IsPokayokesRequired, ValidatedPokayokes)
};

return conditions.Where(c => c.isRequired).All(c => c.validated);
Run Code Online (Sandbox Code Playgroud)