我有多种“猫型”TacoCats、SpaceCats、FatCats。我需要整理我的猫。一个条件是,如果 TacoCats 年龄在 65 岁以下,则不能显示它们。因此,它们必须满足所有其他过滤条件,并且年龄不小于 65。如何更改此表达式,以便只有 TacoCats 受条件影响必须年满 65 岁?为了澄清我需要所有其他猫,无论年龄和条件只影响“TacoCats”

public void SortOutOnlyActiveOrPaidCats()
{
    AllCats = AllCats.Where
        (x =>
            (x.CatStatus == "Active" || x.CatStatus == "Paid")
            && (x.CatCode == "1" || x.CatCode == "2" || x.CatCode == "3" || x.CatCode == "4")
            && (x.CatEnrollmentCode != "G"));
}

这也是我尝试过的原始更改。
 (x = > ((x.CatStatus == "Active" || x.CatStatus == "Paid") && (x.CatCode == "1" || x.CatCode == "2" || x.CatCode == "3" || x.CatCode == "4") && (x.CatEnrollmentCode != "G") && (x.CatType != "TacoCat")) || ((x.CatStatus == "Active" || x.CatStatus == "Paid") && (x.CatCode == "1" || x.CatCode == "2" || x.CatCode == "3" || x.CatCode == "4") && (x.CatEnrollmentCode != "G") && (x.Age >= 65) && (x.PolicyType == "Taco Cats")) )

最佳答案

首先,我们可以通过将状态和代码放入一个数组并使用 Contains 来简化它,然后为 TacoCat 年龄添加额外的条件,如果你简化它,它本质上就是“猫超过 65 岁或者它不是炸 Jade 米饼猫”:

public void SortOutOnlyActiveOrPaidCats()
{
    var validStatuses = new [] { "Active", "Paid" };
    var validCodes = new [] { "1", "2", "3", "4" };

    AllCats = AllCats.Where(x =>
        validStatuses.Contains(x.CatStatus) &&
        validCodes.Contains(x.CatCode) &&
        x.CatEnrollmentCode != "G" &&
        (x.CatType != "TacoCat" || x.Age >= 65)
    );
}

关于c# - 如何从 C# 中选择的排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33044306/

10-13 08:30