启用选项strict时,Linq查询对DataGridViewRow具有隐式转换错误 [英] Linq query has an implicit cast error for DataGridViewRow when option strict is enabled

查看:77
本文介绍了启用选项strict时,Linq查询对DataGridViewRow具有隐式转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView,它绑定到名为"BaseChange"的对象列表. BaseChange对象由4个属性组成...

I have a DataGridView that is bound to a list of objects called "BaseChange". The BaseChange objects are made up of 4 properties...

  • ChangeType
  • ChangeStatus
  • ChangeDescription
  • LastChangeDate

datagridview具有用于所有4个值和第5个值的列(复选框列称为"colIsSelected").将列表绑定到网格并显示项目没有问题.

The datagridview has columns for all 4 values as well as a 5th (a checkbox column called "colIsSelected"). There is no problem binding the list to the grid and displaying the items.

问题在于,启用严格选项时,获取网格中所选项目的查询给我一个隐式强制转换错误.

The problem is that the query that gets the selected items in the grid is giving me an implicit cast error when option strict is enabled.

这是查询...

Dim _changes As List(Of BaseChange)

_changes = (From _row As DataGridViewRow In dgvChanges.Rows() _
            Where Convert.ToBoolean(_row.Cells(NAME_COLUMN_IS_SELECTED).Value) = True _
            Select DirectCast(_row.DataBoundItem, BaseChange)).ToList()

...并且使用严格关闭选项可以产生正确的结果.隐式强制转换形式在"_row As DataGridViewRow"代码上,完整消息为"从'Object'到'System.Windows.Forms.DataGridViewRow'的隐式转换".

...and it produces the correct results with option strict off. The implicit cast squiggle is on the "_row As DataGridViewRow" code, and the full message is "Implicit conversion from 'Object' to 'System.Windows.Forms.DataGridViewRow'".

如果我从查询中排除"As DataGridViewRow",则会在_row.Cells和_row.DataBoundItem上收到后期绑定错误,这也会导致严格的选项失败.

If I exclude the "As DataGridViewRow" from the query, I get a late binding error on the _row.Cells and _row.DataBoundItem and this also fails option strict.

我需要它与启用Option Strict并在VB中一起使用.我在这里想念什么吗?有人有建议吗?

I need this to work with Option Strict enabled, and in VB. Am I missing something here? Anyone have suggestions?

推荐答案

(From _row As DataGridViewRow In dgvChanges.Rows() 

您的_row对象类型必须与集合类型的单个版本匹配.

your type of the _row object has to match the single version of the collection type.

如:

    'Assumes Option Strict On and Option Implicit On
    Dim _changes = (From _row In dgvChanges.Rows() _            
           Where Convert.ToBoolean(ctype(_row,DataGridViewRow).Cells(NAME_COLUMN_IS_SELECTED).Value) = True _           
Select DirectCast(ctype(_row,DataGridViewRow).DataBoundItem, BaseChange)).ToList()

Linq将您的Rows()集合视为IEnumerable,因此您的行是一个对象.底部的说明会更详细.

Linq sees your Rows() collection as IEnumerable, so your row is an object. Explanation at the bottom goes into more detail.

已添加:

添加选项推断应该可以简化此过程.

Adding Option Infer should simplify this.

有关更多详细信息,请参见:

See for more details:

>: //social.msdn.microsoft.com/forums/zh-CN/linqprojectgeneral/thread/e3ec737a-42f8-4767-a190-78390202a991/

说明: 关于它为什么不那么简单,我做了一些更深入的研究.用于DataGridView的RowCollection实现了较旧的IEnumberable接口,该接口返回对象,而较新的集合类型实现了通用的IEnumerable(Of T)接口,该接口直接返回该类型,从而无需进行转换.

Explanation: I did some more digging as to why it isn't simpler. RowCollection for a DataGridView implements the older IEnumberable interface which returns objects, while newer collection types Implement the Generic IEnumerable(Of T) Interface, which returns the type directly, removing the need for casting.

请参见 msdn 用于已实现的接口.

See msdn for implemented interfaces.

这篇关于启用选项strict时,Linq查询对DataGridViewRow具有隐式转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆