Using Linq with 2D array, Select not found

前端 未结 2 794
天命终不由人
天命终不由人 2020-11-27 20:39

I want to use Linq to query a 2D array but I get an error:

Could not find an implementation of the query pattern for source type \'SimpleGame.ILandsca

2条回答
  •  清酒与你
    2020-11-27 21:40

    Your map is a multidimensional array--these do not support LINQ query operations (see more Why do C# Multidimensional arrays not implement IEnumerable?)

    You'll need to either flatten the storage for your array (probably the best way to go for many reasons) or write some custom enumeration code for it:

    public IEnumerable Flatten(T[,] map) {
      for (int row = 0; row < map.GetLength(0); row++) {
        for (int col = 0; col < map.GetLength(1); col++) {
          yield return map[row,col];
        }
      }
    }
    

提交回复
热议问题