简体   繁体   English

C#Linq:从Object列表中返回一个多维数组

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

Say you have a list of Object person: 假设您有一个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? 你可以使用Linq从提到的列表中返回一个多维数组,其中第一个维度是记录索引,第二个维度是名称,第三个维度是电子邮件吗?

Well you can create an object[][] , yes: 那么你可以创建一个object[][] ,是的:

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. 如果你想要一个object[,] ,就我所知,这对常规LINQ来说是不可行的。

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... 这取决于你想对结果做什么,当然......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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