我有一份清单。
可能的成员(x123,y123,z123,a123,b123,c123).// 123是例子 该“mylist”可以包含以x开头的成员,或者可以不包含。对于y,z,a,b,c也是如此。
If contains a member starts with x:
//Formula Contains X
If Not Contains a member starts with x:
//Formula Not Contains X
//same as all of x,y,z,a,b,c. But unlike a foreach, I must place the formulas at checking time, not after.
我该怎么做?
答案 0 :(得分:6)
检查列表中的任何项目是否以“x”开头:
bool result = mylist.Any(o => o.StartsWith("x"))
检查没有项目以“x”列表开头:
bool result = !mylist.Any(o => o.StartsWith("x"));
答案 1 :(得分:5)
答案 2 :(得分:2)
List<string> formula = new List<string> { "x123", "y123" };
string variable = "x";
bool containsVariable = formula.Any(s => s.StartsWith(variable));
答案 3 :(得分:1)
public void Process(List<string> list, string key)
{
if (list.Any(i => i.StartsWith(key)))
{
//Formula Contains key
}
else
{
//Formula Not Contains key
}
}
然后你可以打电话
List<string> list = new List<string> { "x123", "y123", "z123", "a123", "b123", "c123"};
Process(list, "x");
Process(list, "a");