locked
Usin LINQ with Multidimensional Arrays

    Question

  • I have a 2-dimensional array of Integers, declared as

    Private scorepoints as Integer(,)={{1,2},{3,4}}

    I want to use LINQ to get the values of scorepoints(,0) so that I have something like {1,3}. However, because this is a multidimensional array rather than an array of arrays, I cannot find a way to do this other than manually building a new array using a loop. Is there a way to do this using LINQ (or some other way other than a loop)? Thanks.


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, July 10, 2014 11:26 PM

Answers

  • Hi,

    LINQ to Objects is based on the IEnumerable<T> Interface, i.e. a one-dimensional sequence of values. This means it doesn't mix well with n-dimensional data structures like non-jagged arrays, although it's possible. And Select and most other LINQ functions require that the collections they work on implement at least IEnumerable<T> for some T:

    public static IEnumerable<TResult> Select<TSource, TResult>(
        this IEnumerable<TSource> source,
        Func<TSource, TResult> selector
    )

    A rectangular array doesn't implement IEnumerable<T> for any T so it can't be the return value of a Select function.

    But If so define a jagged array:

     int[][] a = { new[] { 1, 2, 3, 4 }, new[] { 5, 6, 7, 8 }, new[] { 9, 10, 11, 12 } };

    You can code like below:

      var col = a.Select(row => row[0]);
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Friday, July 11, 2014 3:03 AM
  • If you are saying I need to rewrite my code as a jagged array, the reason I didn't do that originally (I prefer jagged over multidimensional anyway) is because I wanted to do inline initialization. However, VB.NET interpreted {{1,2},{3,4}} as a multidimensional array rather than a jagged array. I tried the following:
    Private scorepoints As Integer()() = {New Integer() {1, 2}, New Integer() {3, 4}}
    And it worked, but I didn't want to need to write "New Integer()" a million times in my initialization (although I probably would have if I couldn't find anything else). But luckily I found a very interesting solution for the inline initialization:
    Private scorepoints As Integer()() = {({1, 2}), ({3, 4})}
    Apparently the extra parentheses made it interpret it as an array of arrays rather than a multidimensional array. Thank you for your help.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Friday, July 11, 2014 3:37 PM

All replies

  • Hi,

    LINQ to Objects is based on the IEnumerable<T> Interface, i.e. a one-dimensional sequence of values. This means it doesn't mix well with n-dimensional data structures like non-jagged arrays, although it's possible. And Select and most other LINQ functions require that the collections they work on implement at least IEnumerable<T> for some T:

    public static IEnumerable<TResult> Select<TSource, TResult>(
        this IEnumerable<TSource> source,
        Func<TSource, TResult> selector
    )

    A rectangular array doesn't implement IEnumerable<T> for any T so it can't be the return value of a Select function.

    But If so define a jagged array:

     int[][] a = { new[] { 1, 2, 3, 4 }, new[] { 5, 6, 7, 8 }, new[] { 9, 10, 11, 12 } };

    You can code like below:

      var col = a.Select(row => row[0]);
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Friday, July 11, 2014 3:03 AM
  • If you are saying I need to rewrite my code as a jagged array, the reason I didn't do that originally (I prefer jagged over multidimensional anyway) is because I wanted to do inline initialization. However, VB.NET interpreted {{1,2},{3,4}} as a multidimensional array rather than a jagged array. I tried the following:
    Private scorepoints As Integer()() = {New Integer() {1, 2}, New Integer() {3, 4}}
    And it worked, but I didn't want to need to write "New Integer()" a million times in my initialization (although I probably would have if I couldn't find anything else). But luckily I found a very interesting solution for the inline initialization:
    Private scorepoints As Integer()() = {({1, 2}), ({3, 4})}
    Apparently the extra parentheses made it interpret it as an array of arrays rather than a multidimensional array. Thank you for your help.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Friday, July 11, 2014 3:37 PM