从对象列表创建表c#
我有这样一类人:从对象列表创建表c#,c#,visual-studio,class-library,C#,Visual Studio,Class Library,我有这样一类人: 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
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;
}
}
我希望能够从我可以通过电子邮件发送的人员列表中创建一个表,如下所示:
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
列表人员=新列表();
DataTable dt=新的DataTable();
添加(“firstname”,typeof(string));
添加(“lastname”,typeof(string));
添加(“年龄”,类型(int));
foreach(人对人)
{
Add(新对象[]{person.firstName,person.lastName,person.age});
}
}
}
班主任
{
公共字符串名{get;set;}
公共字符串lastName{get;set;}
公共整数{get;set;}
公众人物(字符串名、字符串名、整数)
{
this.firstName=firstName;
this.lastName=lastName;
这个。年龄=年龄;
}
}
}
这不是数据表,您的类的构造函数名称不同。请你的问题提供一个正确的例子。你这样做了吗?你能看一看吗?可能OP指的是一个HTML表格,但同样,我们需要在问题上更清楚一些。@Steve我同意。“通过电子邮件发送”是一个致命的赠品。这太宽泛了。您至少需要自己尝试实现这一点。