如何从C#中选择的排序
本文关键字:选择 排序 | 更新日期: 2023-09-27 18:17:02
我有多种"猫类型"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岁或不是塔可猫":
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)
);
}
public void SortOutOnlyActiveOrPaidCats()
{
AllCats = AllCats.Where
(x =>
x.Age < 65 &&
((x.CatStatus == "Active" || x.CatStatus == "Paid")
&& (x.CatCode == "1" || x.CatCode == "2" || x.CatCode == "3" || x.CatCode == "4")
&& (x.CatEnrollmentCode != "G")));
}
在这里,你首先要确保你只得到65岁以下的猫。然后你使用列出的条件进行过滤
快乐的编码和快乐的抚摸;-(