简体   繁体   中英

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?

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM