为什么编译器认为这是一个对象而不是 DataRow?

发布于 2024-09-12 03:28:20 字数 871 浏览 16 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

⊕婉儿 2024-09-19 03:28:20

请注意 DataTable.Rows - 它是一个 DataRowCollection,仅实现 IEnumerable,而不是 IEnumerable(Of DataRow)

幸运的是, DataTableExtensions 中有一个扩展方法 允许您调用 AsEnumerable( ) 而不是AsEnumerable 返回一个 IEnumerable(Of DataRow):(

Dim qry = From dr As DataRow In dtMessages.AsEnumerable()
          ...

与使用 dt.Rows.Cast(Of DataRow) 相比,我更喜欢这个,因为它提供的更少对可能失败的印象,它更适合DataTable。)

Lok at DataTable.Rows - it's a DataRowCollection which only implements IEnumerable, not IEnumerable(Of DataRow).

Fortunately, there's an extension method in DataTableExtensions which lets you call AsEnumerable() instead of Rows; AsEnumerable returns an IEnumerable(Of DataRow):

Dim qry = From dr As DataRow In dtMessages.AsEnumerable()
          ...

(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 to DataTable. Both will work though.)

度的依靠╰つ 2024-09-19 03:28:20

System.DataTable 类型早于 .Net 中的泛型,因此返回普通的旧 IEnumerable 而不是 IEnumerable(Of DataRow),即使在它们是 DataRow 的实例。因此,上述查询中 dr 的类型是 Object,而不是 DataRow

您可以通过使用 Cast 扩展方法使集合的类型显式来解决此问题

From dr As DataRow in dtMessages.Rows.Cast(Of DataRow)

The type System.DataTable predates generics in .Net and hence returns a plain old IEnumerable instead of an IEnumerable(Of DataRow) even though under the hood they are instances of DataRow. Hence the type of dr in the above query is Object and not DataRow.

You can work around this by using the Cast extension method to make the type of the collection explicit

From dr As DataRow in dtMessages.Rows.Cast(Of DataRow)
椵侞 2024-09-19 03:28:20

DataTable.Rows 属性返回 DataRow 的集合,但该集合不实现 IEnumerable,而是实现 IEnumerable code>,其工作方式为 IEnumerable

您可以使用 dtMessages.Rows.Cast() 将集合项显式转换为 DataRow

The DataTable.Rows property returns a collection of DataRow, however that collection doesn't implement IEnumerable<DataRow> but IEnumerable, which works as IEnumerable<Object>.

You can explicitly cast the collection items to DataRow using dtMessages.Rows.Cast<DataRow>().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文