在 IF 语句 C# 中对多个条件进行分组

本文关键字:条件 语句 IF | 更新日期: 2023-09-27 18:31:15

我正在编写一个程序,需要将搜索作为必需的功能之一执行。用户应该能够使用任意数量的字段,范围从无到全部(总共 7 个)。到目前为止,我已经成功地将每个数据输入案例分组到 if 语句中,如下所示:

List<TypeClass> myList = new List<TypeClass>
foreach TypeClass currentItem in myList
{
    if (data1 == currentItem.GetData1() || data1 == "Do Not Search" && (data2 == currentItem.GetData2() || data2 == "Do Not Search") && (data3...)
    {
        //Stuff
    }
}

如果您注意到,我将每个数据字段分组在括号内,因此仅当输入的每个数据都是所需条件或"空字段"时,才能满足语句。但是,我不能像对其他数据那样对语句的第一部分进行分组2,3,4...相反,即使有其他搜索字段不满足语句的条件,语句也始终被计算为 true。如果我使用额外的括号,程序会完全忽略 if 语句,并将其视为完全不匹配的情况。

所以如果我这样写:

if ((data1 == currentIten.GetData1 || data1 == "Do Not Search") && (data2...)

不检查任何内容,并忽略该语句。这正常吗?是否有更好/更有效的方法来处理可选的搜索字段选择?

编辑:很抱歉拼写错误,每个GetDataX都是一个访问器,我忘了写括号()

在 IF 语句 C# 中对多个条件进行分组

你可以这样对 or 条件这样做

        List<string> mylist = new List<string>();
        string data1 = "test1";
        string data2 = "test2";
        string data3 = "test3";
        string data4 = "test4";

        foreach (string s in mylist)
        {
            bool found = false;
            if(data1.Equals(s) || data1.Equals("Do not Search"))
            {
                found = true;
            }
            if (data2.Equals(s) || data1.Equals("Do not Search"))
            {
                found = true;
            }
            if (data3.Equals(s) || data1.Equals("Do not Search"))
            {
                found = true;
            }
            if (data4.Equals(s) || data1.Equals("Do not Search"))
            {
                found = true;
            }

        }

或者像这样用于和条件

        List<string> mylist = new List<string>();
        string data1 = "test1";
        string data2 = "test2";
        string data3 = "test3";
        string data4 = "test4";

        foreach (string s in mylist)
        {
            bool found = false;
            bool notfound = false;
            if(data1.Equals(s) || data1.Equals("Do not Search"))
            {
                found = true;
            }
            else
            {
                notfound = true;
            }
            if (data2.Equals(s) || data1.Equals("Do not Search"))
            {
                found = true;
            }
            else
            {
                notfound = true;
            }
            if (data3.Equals(s) || data1.Equals("Do not Search"))
            {
                found = true;
            }
            else
            {
                notfound = true;
            }
            if (data4.Equals(s) || data1.Equals("Do not Search"))
            {
                found = true;
            }
            else
            {
                notfound = true;
            }
            // Force all to match
            if (notfound)
                return null;
        }

我的偏好是这样的,尽管您可以利用搜索功能来做您需要做的事情......

List<string> mylist = new List<string>();
        List<string> mysearches = new List<string>();
        string data1 = "test1";
        string data2 = "test2";
        string data3 = "test3";
        string data4 = "test4";
        if(data1 != "Do not Search")
            mysearches.Add(data1);
        if (data2 != "Do not Search")
            mysearches.Add(data2);
        if (data3 != "Do not Search")
            mysearches.Add(data3);
        if (data4 != "Do not Search")
            mysearches.Add(data4);
        bool found = false;
        bool andconditionmatch = true;
        foreach (string s in mylist)
        {
            if (mysearches.Contains(s))
            {
                found = true;
            }
            else
            {
                andconditionmatch = false;
            }
        }

将所有可能性放在哈希集中。

Hashset with all possibilities.
foreach(item in selectedItems) //you may not need this if you dont have to perform action forall selected items.
{
     if (Hashset contains item)
       //do stuff.
}

我会在类内移动匹配,而不是处理 7 个单独的可能值进行匹配,而是将它们作为一个名为 criteria 的类实例。请参阅下面的示例代码:

public enum States
{
    None,
    Tenesee,
    Georgia,
    Colorado,
    Florida
}
class Item
{
    public States State { get; set; }
    public string Name { get; set; }
    public int ID { get; set; }
    public bool IsMatch(Item criteria)
    {
        bool match = true;
        if (criteria.State != States.None) match &= criteria.State == State;
        if (!string.IsNullOrEmpty(criteria.Name)) match &= criteria.Name.Equals(Name);
        if (criteria.ID > 0) match &= criteria.ID == ID;
        return match;
    }
    public override string ToString()
    {
        return string.Format("ID={0}, Name={1}, State={2}", ID.ToString(), Name, State.ToString());
    }
}
class Program
{
    static void Main(string[] args)
    {
        List<Item> list = new List<Item>();
        list.Add(new Item() { ID = 10016, Name = "Julia", State = States.Georgia });
        list.Add(new Item() { ID = 10017, Name = "Scott", State = States.Colorado });
        list.Add(new Item() { ID = 10018, Name = "Samantha", State = States.Tenesee });
        list.Add(new Item() { ID = 10019, Name = "Julia", State = States.Florida });

        Item criteria = new Item()
        {
            State = States.Tenesee,
            ID = 10018
        };
        List<Item> selection = list.FindAll((item) => item.IsMatch(criteria));
        foreach (var item in selection)
        {
            Console.WriteLine("{0}", item);
        }
    }
}

随着结果 ID=10018, Name=Samantha, State=Tenesee

因此,您可以构建一个条件实例Item并在属性定义良好的情况下比较匹配项。循环遍历所有项目并选择与条件匹配的项目。显然,您必须为所有 7 个属性扩展.IsMatch()