asp.net - Option Strict 不允许使用 IEnumerable 进行后期绑定
问题描述
Dim distinctJoints As IEnumerable
distinctJoints = From row In spotsTable.AsEnumerable()
Group row By Key = row("FUNCJOINTCODE") Into Group
Select Key, Group
_evaluatedJointsCount = (From row In spotsTable.AsEnumerable()
Group row By Key = row("FUNCJOINTCODE") Into Group
Select Key, Group).Count()
'Process each joint
For Each currentJoint In distinctJoints
Dim currentJointKey As String = currentJoint.Key
因为上面的代码currentJoint.Key
在选项严格打开后显示后期绑定错误。你能帮我解决这个问题吗?
解决方案
首先,让我祝贺您将代码移向Option Strict On
! 一开始可能会做一些工作,但从长远来看会有所回报,因为会在编译时而不是在运行时发现很多错误。
也就是说,让我们看看你的问题。这里:
Dim distinctJoints As IEnumerable
您声明distinctJoints
为非通用IEnumerable
. 非泛型 IEnumerableObject
在迭代时返回类型项。该类型Object
不包含Key
方法。这就是您收到编译时错误的原因。
由于您的 LINQ 查询返回匿名类型的通用 IEnumerable,因此解决方案是改用类型推断。在您的项目属性中激活Option Infer On
(如果您尚未这样做)并让编译器推断正确的数据类型:
' Dim distinctJoints As IEnumerable <-- remove this
Dim distinctJoints = From row In spotsTable.AsEnumerable()
Group row By Key = row("FUNCJOINTCODE") Into Group
Select Key, Group
推荐阅读
- java - Java - 读取压缩位图
- extjs - ExtJS 4.1 树形面板 - 扩展树形面板会导致重复记录
- simulink - Raspberry Pi 音频文件在 Simulink 中读取多个音频文件的模块
- c - 如何在 c 中对数组进行子集化?
- android - Facebook Native in feed 广告在 RecyclerView 中相互重叠
- vue.js - 请求后vuejs自动刷新列表
- graph-algorithm - 图中最长的非重复路径?
- javascript - 如何为“活动”元素添加事件侦听器
- java - Java Threadpool 处理无限排队线程?
- dropzone - DropZone parallelUploads 不工作我已将 parallelUploads 设置为 10,但它在一个请求中上传 20