我正在编写一个程序,它需要执行搜索作为所需的函数之一。用户应该能够使用任意数量的字段,范围从无到所有字段(总共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语句,并将其视为没有一个case匹配。
所以如果我这样写的话:
if ((data1 == currentIten.GetData1 || data1 == "Do Not Search") && (data2...)不检查任何内容,并且忽略该语句。这是正常的吗?有没有更好/更有效的方法来处理可选的搜索字段选择?
编辑:很抱歉输入错误,每个GetDataX都是一个访问器,我忘了写括号()
发布于 2012-04-17 07:12:01
对于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;
}
}或者像这样的and条件
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-17 07:19:58
把所有的可能性都放在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-17 07:43:01
我会将匹配移到类中,而不是处理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
因此,您构建一个criteria实例Item,并在属性定义良好的情况下进行匹配比较。循环遍历所有项目,并选择符合条件的项目。显然,您必须为所有7个属性扩展.IsMatch()。
https://stackoverflow.com/questions/10182834
复制相似问题