问题描述
我有一群看起来像这样的人:
class Person
{
public string firstName { get; }
public string lastName { get; }
public int age { get; set; }
public Person(string firstName,string lastName,int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
我希望能够通过可以通过电子邮件发送的人员列表创建一个表格,如下所示:
我该怎么办?
谢谢
解决方法
尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Person> people = new List<Person>();
DataTable dt = new DataTable();
dt.Columns.Add("firstname",typeof(string));
dt.Columns.Add("lastname",typeof(string));
dt.Columns.Add("age",typeof(int));
foreach (Person person in people)
{
dt.Rows.Add(new object[] { person.firstName,person.lastName,person.age });
}
}
}
class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
public Person(string firstName,string lastName,int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
}