【发布时间】:2020-04-03 02:27:56
【问题描述】:
我正在尝试将 DataSet 转换为 IList<myDataModel> 并在尝试填充行号列时卡住了。
这是我转换数据的方法:
private IList<Web_Notes.Models.NotesRequested> DataSetToList(DataSet ds)
{
int currentBatch = GetCurrentBatchId();
var notesList = ds.Tables[0].AsEnumerable().Select(dataRow => new Web_Notes.Models.NotesRequested
{
batch_id = currentBatch,
//rowNumber = index of current row
note_type = dataRow.Field<string>("Note Type"),
note_system = dataRow.Field<string>("Note System"),
note_text = dataRow.Field<string>("Note Text"),
country = dataRow.Field<string>("Country")
}).ToList();
return notesList;
}
note 列由用户输入,batch_id 和 rowNumber 是计算列。
到目前为止一切正常,除了rowNumber
这是预期的结果
batch_id rowNumber note_type note_system note_text country
1 1 note system text cntry
1 2 note system text cntry
1 3 note system text cntry
1 4 note system text cntry
1 5 note system text cntry
1 6 note system text cntry
我可以使用ds,Tables[0].Rows.IndexOf(row);获取行号
但我不知道如何在这种情况下应用它,因为dataRow 似乎没有IndexOf() 属性。
【问题讨论】:
标签: c# list linq enumerable