You can't query against the DataTable
's Rows collection, since DataRowCollection
doesn't implement IEnumerable<T>
.
(您无法查询DataTable
的Rows集合,因为DataRowCollection
没有实现IEnumerable<T>
。)
You need to use the AsEnumerable()
extension for DataTable
. (您需要为DataTable
使用AsEnumerable()
扩展。)
Like so: (像这样:)
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;
And as Keith says, you'll need to add a reference to System.Data.DataSetExtensions
(正如Keith所说,您需要添加对System.Data.DataSetExtensions的引用)
AsEnumerable()
returns IEnumerable<DataRow>
.
(AsEnumerable()
返回IEnumerable<DataRow>
。)
If you need to convert IEnumerable<DataRow>
to a DataTable
, use the CopyToDataTable()
extension. (如果需要将IEnumerable<DataRow>
转换为DataTable
,请使用CopyToDataTable()
扩展名。)
Below is query with Lambda Expression,
(以下是Lambda表达式查询,)
var result = myDataTable
.AsEnumerable()
.Where(myRow => myRow.Field<int>("RowNo") == 1);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…