1

I'm trying to deep copy a 2D array using Linq and the Select method based off a code sample I found:

var copy = original.Select(x => x.ToArray()).ToArray(); where "original" is int[,] original = new int[10, 10] and filled with values.

However, when I attempt using this format, I get a definition error for Select() as if it just isn't there to begin with. Note, I do have using System.Linq; listed. The Linq methods don't even show in the Intellisense popup window while typing.

I assume my 2D array itself isn't an issue, since I've filled it and printed all the values without issue, so it isn't broken. But why are Linq methods not working despite being included in the code library list?

Photon
  • 9
  • 5
  • The ```original``` 2D array is ```int``` and I'm copying into ```var copy```. Shouldn't that just set the copy to the correct data type of the original? I did manually change the copy to an int as well and it didn't affect ```original.Select()``` not being recognized. – Photon Feb 01 '20 at 19:04
  • Wait, can you not use Linq with normal arrays? The example Linq I listed in the question was specifically used in an example with 2D arrays instead of lists, and doesn't even have Collections included. – Photon Feb 01 '20 at 19:07
  • It shows similar information as the other user who posted about ```Cast()```, however, that causes another issue with the ```ToArray()``` call. Does that work with 2D arrays or just 1D? – Photon Feb 01 '20 at 19:16
  • 1
    You cannot call `ToArray` because there is no subsequence to be converted to an array. What you are thinking about is possible with [jagged arrays](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays), not 2D arrays. If you took advice from [here](https://stackoverflow.com/a/15725856/11683), then unfortunately they are talking about jagged arrays, incorrectly calling them 2D arrays. – GSerg Feb 01 '20 at 19:19
  • Is that the difference between using ```int[][]``` and ```int[,]```? If so, then that makes sense as the example I was following was the former. – Photon Feb 01 '20 at 19:21
  • Yes, that is the difference. – GSerg Feb 01 '20 at 19:22

1 Answers1

0

Cast your array to IEnumerable

original.Cast<int>().Select(x => x ).ToArray();

The above query will flatten your 2d array to a single dimensional array. I do not know what your desired result would be. I just tried to clear up your blocker using Linq methods over 2d arrays

Beingnin
  • 2,288
  • 1
  • 21
  • 37
  • Which makes it a 1D array. – GSerg Feb 01 '20 at 19:11
  • @GSerg exactly. – Beingnin Feb 01 '20 at 19:13
  • This does seem to fix the problem with Select() not being recognized, but in turn, it breaks one of the ToArray() calls with the same definition error. If I remove it, the whole line becomes a conversion error. I'm trying to copy a 2D array to a new 2D array, so I can use the copy instead of the original. – Photon Feb 01 '20 at 19:13
  • @Photon What is the purpose of that `ToArray` method inside the `Select`? Instead of an array, it will be an `IEnumerable`, which itself can do everything you need to work on an array – Beingnin Feb 01 '20 at 19:16
  • I'm going over a 2D array? Shouldn't I have an extra one? Does only one ```ToArray()``` at the very end cover both rows and columns? – Photon Feb 01 '20 at 19:18
  • The problem is that linq queries are implemented on `IEnumerable` only. Unfortunately 2d arrays does not implement that. The only way to use them is to convert them to an `IEnumerable` or you need to write custom logics, mostly nested loops to get what you want – Beingnin Feb 01 '20 at 19:22
  • From other comments, I found that the original example I followed was with jagged arrays instead of 2D arrays (incorrectly calling them as such). Will Linq and casting still work here for my case, then? – Photon Feb 01 '20 at 19:25
  • Yes you are not using jagged arrays here and so this will work using cast – Beingnin Feb 01 '20 at 19:26