【发布时间】:2009-10-27 20:56:50
【问题描述】:
我想将 DataTable 转换为 IEnumerable<> 或 Dictionary<string, object>。我尝试了以下 LINQ 查询,
from DataRow row in ds.Tables[0].AsEnumerable()
let rowDictionary = new Dictionary<string, object>()
from DataColumn column in row.Table.Columns.Cast<DataColumn>()
select rowDictionary.Add(column.ColumnName, row[column]).ToArray();
但我收到以下错误:
error CS1943: An expression of type
'System.Collections.Generic.IEnumerable<System.Data.DataColumn>' is not
allowed in a subsequent from clause in a query expression with source type
'System.Data.EnumerableRowCollection<AnonymousType#1>'. Type inference
failed in the call to 'SelectMany'.
我知道我可以通过循环强制执行此操作,但这似乎是我应该能够在 LINQ 中执行的操作。提前感谢您的帮助!
【问题讨论】: