![](/img/trans.png)
[英]issue looping through each value of a 2D Jagged Array “Index was outside the bounds of the array”
[英]LINQ - looping through rows of 2d array
我有一個像這樣的二維字符串數組:
string myarr[,] = new string[100,4];
我想獲取的行數在第二列上包含值vpp
。
即,使用正常循環,我將這樣做:
int s = 0;
for(int i=0; i<100; i++)
{
if( myarr[i,1] == "vpp" )
{
s++;
}
}
我們如何使用LINQ做到這一點?
我相信,這將遍歷2d數組的所有元素,而不是逐行循環:
s = (from string myRow in myarr where myRow == "vpp"
select myRow).Count();
由於我沒有訪問Visual Studio的權限,因此現在正在移動設備上(旅行和閱讀),因此我只鍵入了上面的代碼。
謝謝
var count = Enumerable.Range(0, myarr.GetUpperBound(0)+1)
.Count(r => myarr[r,1] == "vpp");
var s = Enumerable.Range(0, myarr.GetLength(0)).Count(index => myarr[index, 1] == "vpp");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.