c# - 如果条件与数组中的每个项目
问题描述
我的英语不是很好。所以我厌倦了用数组中的每个项目写 if 条件。这个有什么解决办法。谢谢你的帮助这段代码让我很累,所以怎么做
public bool AllowElement(Element element)// element is object by mouse houver on Revit program
{
BuiltInCategory[] BIC = new BuiltInCategory[]{Frame, Column, Slab,Foundation, etc...}// BIC is
in enum, and this is declare with params key word
if( ele == (int)BIC.Frame || ele == (int)BIC.Column ||
ele == (int)BIC.Slab || ele == (int)BIC.Foundation ||
ele == (int)BIC.etc....)
{
return true
}
}
结束我写的这段代码,但我觉得它不是真的
public bool AllowElement(Element element)
{
int ele = element.Category.Id.IntegerValue;
foreach (var item in BIC)
{
if (ele == (int)item)
return true;
}
return false;
}
解决方案
您是否尝试过使用 Linq 来验证该项目是否存在?
public bool AllowElement(Element element)
{
return BIC.Any(x => (int)x == element.Category.Id.IntegerValue);
}
Any()
如果有任何元素符合条件 Microsoft docs on Any
这是假设 BIC 在此方法的范围内。
推荐阅读
- scala - 在 Scala 中使用 QueryParamDecoder 解码可选的查询参数
- python - 在 Kivy 应用程序启动时满足特定条件时如何自动弹出警报
- arrays - 如何从 JSON 数组中返回特定范围的索引?
- python - ValueError:要解包的值太多(预期为 2):pandas
- java - 使用公钥解码和验证 JWS 令牌
- flutter - 列小部件中的 SingleChildScrollView
- c# - 页面调整大小后的目录问题
- web - 如何处理增量资金
- node.js - 归档(删除)MongoDB 数据的最佳方式
- javascript - *ngFor 根本不使用对象数组