0

我正在编写一个需要执行搜索作为必需功能之一的程序。用户应该能够使用任意数量的字段,范围从没有到全部(总共 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 那样对语句的第一部分进行分组...相反,即使存在其他不满足条件的搜索字段,该语句也总是被评估为真该声明。如果我使用额外的括号,程序将完全忽略 if 语句并将其视为根本没有任何情况匹配。

所以如果我这样写:

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

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

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

4

3 回答 3

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;
            }
        }
于 2012-04-16T23:12:01.377 回答
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.
}
于 2012-04-16T23:19:58.073 回答
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并比较是否有明确定义的属性。循环遍历所有项目并选择符合条件的项目。显然,您必须扩展.IsMatch()所有 7 个属性。

于 2012-04-16T23:43:00.640 回答