Saeid
Saeid

Reputation: 693

C# Linq: Return a multidimensional array from a list of Object

Say you have a list of Object person:

private List<Person> lstPersons = new List<Person>();

In which person is defined as:

public class Person
{
      public string Name { get; set; }

      public int Age { get; set; }

      public string Mail { get; set; }
}

Can you use Linq to return a multidimensional array from the mentioned list in which the first dimension is index of record, the second dimension is name and the third dimension is email?

Upvotes: 2

Views: 1958

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500805

Well you can create an object[][], yes:

object[][] array = people.Select((p, index) => new object[] { index, p.Name, p.Mail })
                         .ToArray();

If you wanted an object[,], that's not doable with regular LINQ as far as I'm aware.

If you have the choice though, I'd personally use an anoymous type:

var projected = people.Select((p, index) => new { Index = index, p.Name, p.Mail })
                      .ToArray();

It depends on what you want to do with the result, of course...

Upvotes: 4

Related Questions