C # LINQ query for multidimensional array

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.

        // Create an multidimensional array, then just put numbers into it and print the array.  
        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.

+4
1

, () IEnumerable, IEnumerable<T>. , Cast - Cast , :

var highList = from int val in myArr where (val > 5) select val;

:

var highList = from int val in myArr where val > 5 select val;

, , :

var highList = myArr.Cast<int>().Where(val => val > 5);

, . Cast, :

public static class RectangularArrayExtensions
{
    public static IEnumerable<T> Cast<T>(this T[,] source)
    {
        foreach (T item in source)
        {
            yield return item;
        }
    }
}
+10

All Articles