0

I have a class

public class SomeClass {
            public string Name { get; set; }
            public string Age{get;set;}
        }

I have an array declared

string[,] testArray = { { "Vignesh", "25" },{"Raja","20"} };

I tried to convert the testArray to object of SomeClass[] using linq to sql But testArray.Select( is not coming. It says String[*,*] does not contain a definition for Select

var result = linqArray.Cast<string>().Select(x => x).ToArray();
            linqArray.Cast<string>()
 .Select(x => new SomeClass { Name =x, Age = x })
 .ToArray();

When I try this it is result is like shown below

enter image description here

I even tried type casting, which didnt work

SomeClass[] list = (SomeClass[])linqArray;
Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150

1 Answers1

3

It's because multidimensional arrays are not IEnumerable<T>.First you need to turn your array into IEnumerable<T> using Cast or OfType method, then you can use Batch method:

array.Cast<string>()
 .Batch(2)
 .Select(x => new SomeClass { Name = x.First(), Age = x.Last()) })
 .ToArray();

Note you need to add a reference to MoreLINQ library to use Batch method.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184