开发者

Can we convert the list into DataTable in C#?

开发者 https://www.devze.com 2023-01-17 13:32 出处:网络
Can any on开发者_StackOverflow社区e tell me how to convert the List into data table in C# ?Do you mean generically? There is no automatic way to do it.

Can any on开发者_StackOverflow社区e tell me how to convert the List into data table in C# ?


Do you mean generically? There is no automatic way to do it.

But you can manually:

class Person
{
  public int ID {get; set;}
  public string FirstName {get; set;}
...
}

var personsList = new List<Person>();
var dataTable = new DataTable();
dataTable.Columns.Add("ÏD", typeof(int));
dataTable.Columns.Add("FirstName", typeof(string));
...

foreach (var person in personsList)
{
  dataTable.Rows.Add(person.ID, person.FirstName...)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消