在List <string>中使用StartsWith和Contains?

ith*_*que 4 c# list

我有一份清单.

可能的成员(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.
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Fab*_*ler 8

检查列表中的任何项目是否以"x"开头:

bool result = mylist.Any(o => o.StartsWith("x"))
Run Code Online (Sandbox Code Playgroud)

检查没有项目以"x"列表开头:

bool result = !mylist.Any(o => o.StartsWith("x"));
Run Code Online (Sandbox Code Playgroud)


Bru*_*oLM 5

您可以使用.AnyLinq

bool result = mylist.Any(o => o.StartsWith("x"));
Run Code Online (Sandbox Code Playgroud)

这将迭代列表并告诉您是否至少有一个元素以"x"开头