how to use the following LINQ query correctly? I want to create a one-dimensional array that contains only values that are greater than 5. I cannot understand why it cannot iterate over this multidimensional array, but if I use "foreach", it will actually be iterated.
int[,] myArr = new int[5,6];
int rows = myArr.GetLength(0);
int cols = myArr.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int k = 0; k < cols; k++)
{
myArr[i,k] = i + k;
Console.Write(myArr[i,k]);
}
Console.WriteLine("");
}
var highList = from val in myArr where (val > 5) select val;
Error: Error 1 Could not find the implementation of the query template for the source type 'int [,]'. "Where" not found. Are you missing the links or usage guidelines for "System.Linq"?
I thought this could solve the problem:
public static IEnumerator<int> GetEnumerator(int[,] arr)
{
foreach(int i in arr)
{
yield return i;
}
}
But I assume that it really does not implement an iterator. Sorry if I'm talking about strange things here, I'm a little new to these things ... Thanks.