如何使用C#将列表转换为数据集?
//
// Conventional way
//
private DataSet GetDataSetConventional(List<FileSearchResultItem> list)
{
DataSet _result = new DataSet();
_result.Tables.Add("results");
_result.Tables("results").Columns.Add("A");
_result.Tables("results").Columns.Add("B");
_result.Tables("results").Columns.Add("C");
_result.Tables("results").Columns.Add("D");
_result.Tables("results").Columns.Add("E");
_result.Tables("results").Columns.Add("F");
_result.Tables("results").Columns.Add("G");
foreach (FileSearchResultItem item in list) {
DataRow newRow = _result.Tables("results").NewRow();
newRow("A") = item.Index;
newRow("B") = item.Image;
newRow("C") = item.Name;
newRow("D") = item.Size;
newRow("E") = item.Files;
newRow("F") = item.IsDirectory;
_result.Tables("results").Rows.Add(newRow);
}
return _result;
}
Take a look here
You basically use a for loop to go through the list and map the list items to DataTable items creating a new row each iteration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.