1

I try to order this 2d-array [10000, 150] with LINQ. I want to order it dependent of the second column. And I can't use a one-dimensional array.

namespace test2 {
    class Test {
        public static void Main() {
            string[,] array = new string[,]
            {
                {"cat", "dog"},
                {"bird", "fish"},
            };
            array = array.OrderBy (a => a [1]);
        }
    }
}

But I get the error: /home/Program.cs(18,18): Error CS0411: The type arguments for method `System.Linq.Enumerable.OrderBy(this System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly (CS0411) (test2)

How can I specify the type arguments?

Thanks for your help!

kame
  • 20,848
  • 33
  • 104
  • 159
  • 1
    @Ganesh_Devlekar it won't work. LINQ extension methods cannot be applied to multidimensial array. Try snippets from http://stackoverflow.com/questions/8866414/how-to-sort-2d-array-in-c-sharp instead. –  May 03 '15 at 18:39
  • @pwas is right, you can find discussion about this topic useful in http://stackoverflow.com/questions/275073/why-do-c-sharp-multidimensional-arrays-not-implement-ienumerablet question – Victor May 03 '15 at 18:41
  • But I use a 10000 x 150 Array, so some methods doesn't work. I can't give a number to all the columns. I also can't use a one dimensional array. – kame May 03 '15 at 18:43
  • So what about the `string[][]` solution? – Emil Ingerslev May 03 '15 at 18:46

1 Answers1

2

LINQ functions does not work on multidimensional arrays. You could just convert it to this:

string[][] array = new string[][]
{
    new [] {"cat", "dog"},
    new [] {"bird", "fish"},
};

var result = array.OrderBy(a => a[1]);
Emil Ingerslev
  • 4,645
  • 2
  • 24
  • 18