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

时间:2012-04-16 22:53:24

标签: c# if-statement conditional-statements

我正在编写一个需要执行搜索的程序作为必需的功能之一。用户应该能够使用任意数量的字段,范围从none到all(总共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都是一个访问者,我忘了写括号()

3 个答案:

答案 0 :(得分:2)

你可以这样做或条件

        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;
            }
        }

答案 1 :(得分:0)

将所有可能性放在hashset中。

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.
}

答案 2 :(得分:0)

我会在类中移动匹配,而不是处理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()