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;
6
  • I think this needs some more clarity, I'm pretty sure the the second c# snippet isn't proper c# Commented Feb 23, 2015 at 11:11
  • 1
    using System.Linq; ? Commented Feb 23, 2015 at 11:13
  • Why do you use a mutli-dimentsional array in the first place? Commented Feb 23, 2015 at 11:14
  • Yes @Sinatr it is there Commented Feb 23, 2015 at 11:14
  • 1
    @netwer: Often these questions are a cutdown version of the full code. In this case that might work but in reality I would guess that SomeClass probably has more properties on it. The nature of SomeClass is not important to the question, just the fact that it is in a multidimensional array. Commented Feb 23, 2015 at 11:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.