![](/img/trans.png)
[英]How do I get all elements of a certain category, type and familiy in revit
[英]How do I find all elements in an array of a certain type?
我以前從未使用過謂詞,因此在使用Array.FindAll時遇到了很多麻煩
基本上,我在這里有一個自己的自定義枚舉數組。public enum Colors {空,藍色,紅色,綠色,紫色,白色,橙色};
我的問題是我不明白如何使用FindAll來計算數組中某種顏色出現的次數。 我打算做這樣的事情...
Colors matches[];
matches = Array.FindAll(myArray,/*I don't know what to put here*/);
int numOfMatches = matches.length;
例如,我如何將第二個參數用作查找像藍色這樣的顏色?
您需要傳遞一個謂詞。 在這種情況下,您將檢查給定的顏色是否等於您感興趣的顏色:
var matches = Array.FindAll(myArray, c => c == someColor);
您還可以使用Enumerable.Count
方法查找匹配項的數量(如果以后不需要將其用於其他任何事情):
var numOfMatches = myArray.Count(c => c == someColor);
您可以使用LINQ來做到這一點:
int numOfBlue = matches.Count(m => m == Colors.Blue);
它只會返回一個數字,而不是返回帶有匹配元素的新數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.