gpt4 book ai didi

C# 林奇 : Return a multidimensional array from a list of Object

转载 作者:太空宇宙 更新时间:2023-11-03 23:26:45 27 4
gpt4 key购买 nike

假设你有一个 Object person 的列表:

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

其中person定义为:

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

public int Age { get; set; }

public string Mail { get; set; }
}

您能否使用 Linq 从上述列表中返回一个多维数组,其中第一个维度是记录的索引,第二个维度是名称,第三个维度是电子邮件?

最佳答案

你可以创建一个 object[][],是的:

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

如果您想要一个 object[,],据我所知,这对于常规 LINQ 是行不通的。

如果您可以选择,我个人会使用匿名类型:

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

这取决于你想对结果做什么,当然...

关于C# 林奇 : Return a multidimensional array from a list of Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33587987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com