为什么编译器认为这是一个对象而不是 DataRow?
我使用 LINQ 查询将 DataTable
对象内的数据转换为自定义 POCO 对象的简单 IEnumerable
。
我的 LINQ 查询是:
Dim dtMessages As DataTable
'...dtMessages is instantiated ByRef in a helper data access routine... '
Dim qry = From dr As DataRow In dtMessages.Rows
Select New OutboxMsg With {
.ComputerID = dr(dcComputerID),
.MessageData = dr(dcMessageData),
.OutBoxID = dr(dcOutBoxID),
.OutBoxReceivedID = dr(dcOutBoxReceivedID),
.TrxDate = dr(dcTrxDate)
}
但是,编译器在 dr As DataRow
下抛出一条警告消息,其中包含以下消息:
Option Strict On 不允许从“Object”到“System.Data.DataRow”的隐式转换。
为什么我会收到此错误?我需要做什么来修复它?我本以为dtMessages.Rows
返回一个DataRow
类型的集合。这不正确吗?
I'm using a LINQ query to translate data inside a DataTable
object to be a simple IEnumerable
of a custom POCO object.
My LINQ query is:
Dim dtMessages As DataTable
'...dtMessages is instantiated ByRef in a helper data access routine... '
Dim qry = From dr As DataRow In dtMessages.Rows
Select New OutboxMsg With {
.ComputerID = dr(dcComputerID),
.MessageData = dr(dcMessageData),
.OutBoxID = dr(dcOutBoxID),
.OutBoxReceivedID = dr(dcOutBoxReceivedID),
.TrxDate = dr(dcTrxDate)
}
However, the compiler is throwing a warning message under dr As DataRow
with the message:
Option Strict On disallows implicit conversions from 'Object' to 'System.Data.DataRow'.
Why am I getting this error and what do I need to do to fix it? I would have thought that dtMessages.Rows
returned a collection of type DataRow
. Is this not correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意
DataTable.Rows
- 它是一个DataRowCollection
,仅实现IEnumerable
,而不是IEnumerable(Of DataRow)
。幸运的是,
DataTableExtensions
中有一个扩展方法 允许您调用AsEnumerable( )
而不是行
;AsEnumerable
返回一个IEnumerable(Of DataRow)
:(与使用
dt.Rows.Cast(Of DataRow)
相比,我更喜欢这个,因为它提供的更少对可能失败的印象,它更适合DataTable
。)Lok at
DataTable.Rows
- it's aDataRowCollection
which only implementsIEnumerable
, notIEnumerable(Of DataRow)
.Fortunately, there's an extension method in
DataTableExtensions
which lets you callAsEnumerable()
instead ofRows
;AsEnumerable
returns anIEnumerable(Of DataRow)
:(I prefer this over using
dt.Rows.Cast(Of DataRow)
, as it gives less of an impression of something which might fail. It's more tailored toDataTable
. Both will work though.)System.DataTable
类型早于 .Net 中的泛型,因此返回普通的旧IEnumerable
而不是IEnumerable(Of DataRow)
,即使在它们是 DataRow 的实例。因此,上述查询中dr
的类型是Object
,而不是DataRow
。您可以通过使用
Cast
扩展方法使集合的类型显式来解决此问题The type
System.DataTable
predates generics in .Net and hence returns a plain oldIEnumerable
instead of anIEnumerable(Of DataRow)
even though under the hood they are instances ofDataRow
. Hence the type ofdr
in the above query isObject
and notDataRow
.You can work around this by using the
Cast
extension method to make the type of the collection explicitDataTable.Rows
属性返回DataRow
的集合,但该集合不实现IEnumerable
,而是实现IEnumerable
code>,其工作方式为IEnumerable
您可以使用
dtMessages.Rows.Cast()
将集合项显式转换为DataRow
。The
DataTable.Rows
property returns a collection ofDataRow
, however that collection doesn't implementIEnumerable<DataRow>
butIEnumerable
, which works asIEnumerable<Object>
.You can explicitly cast the collection items to
DataRow
usingdtMessages.Rows.Cast<DataRow>()
.