从对象列表中创建 Table c#
Creating a Table from a list of objects c#
我有 Class 个人是这样的:
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;
}
}
我希望能够从我可以通过电子邮件发送的人员列表中创建一个 table,如下所示:
我该怎么做?
谢谢
尝试以下操作:
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;
}
}
}
我有 Class 个人是这样的:
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;
}
}
我希望能够从我可以通过电子邮件发送的人员列表中创建一个 table,如下所示:
我该怎么做?
谢谢
尝试以下操作:
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;
}
}
}