我有一些像这样的c#行:
if (justification.Contains("CT_") ||
justification.Contains("CTPD_") ||
justification.Contains("PACS_") ||
justification.Contains("NMG_") ||
justification.Contains("TFS_ID") ||
justification.Contains("AX_") ||
justification.Contains("MR_") ||
justification.Contains("FALSE_POSITIVE") ||
justification.Contains("EXPLICIT_ARCH_TEAM_DESIGN_DECISON") ||
justification.Contains("EXPLICIT_ARCH_TEAM_DESIGN_DECISION"))
{
// justification is ok.
}else
{
reporter.Report(syntaxNode.GetLocation(), syntaxNode,
Resources.SpecifySuppressMessageJustificationTitle);
}
我的想法是将所有这些字符串放入arra和我的IF表达式中,我只是迭代我的数组(或枚举IEnumerable)。但是我怎么能这样做呢?
我从这里开始:
IEnumerable<string> someValues = new List<string>() { "CT_", "CTPD","PACS_", "NMG_", "AX_" };
if (justification == BUT HOW I HAVE TO RUN THROUGH MY someValuesand get the
stringValues?)
{
}
答案 0 :(得分:6)
你可以这样做,如果你需要不区分大小写包含检查你可以使用justification.ToUpper()
得到大写,因为你已经有大写的值列表
var someValues = new List<string>() { "CT_", "CTPD","PACS_", "NMG_", "AX_" };
if(someValues.Any(x=>justification.Contains(x))
{
// justification is ok.
}else
{
// not matching
}
答案 1 :(得分:5)
var someValues = new[] { "CT_", "CTPD","PACS_", "NMG_", "AX_" };
if (someValues.Any(x => justification.Contains(x))
{
// justification is ok.
}
答案 2 :(得分:0)
如果理由是IEnumerable(或列表),您可以使用Intersect
拦截会产生 集 可在两个列表中找到的项目。
IEnumerable<string> someValues = new List<string>() { "CTPD","PACS_", "NMG_", "AX_"};
if (justification.Intersect(someValues).Any())
{
// Atleast one match was found.
}
如果对齐变量是字符串,则可以使用Any()
if(someValues.Any(x => justification.Contains(x))
{
// A match was found.
}