vb 与 linq-to-sql 的接口
我在转换和界面方面遇到问题。下面是我正在使用的代码。错误说了什么。我不完全明白发生了什么事。我认为,如果我创建了一个实现接口的项目列表,如果我传递另一个也实现该接口的项目,那么它应该可以工作。但事实并非如此。有人可以解释为什么不这样做以及我应该做什么。非常感谢
错误行有三个 astrix 来识别它。再次非常感谢
未处理的类型异常 发生“System.InvalidCastException” 在DAL.dll中
附加信息:无法投射 类型的对象 'System.Data.Linq.DataQuery<代码>1[BuisnessObjects.Project]' 键入 'System.Collections.Generic.IEnumerable1[BuisnessObjects.IProject]'
Imports BuisnessObjects
Public Class ProjectInfoRepository
Implements IProjectInfoRepository
Function GetAllProjects() As List(Of BuisnessObjects.IProject) Implements IProjectInfoRepository.GetAllProjects
Dim returnList As New List(Of BuisnessObjects.IProject)
***returnList.AddRange(From p In DC.ProjectInfos _
Select New BuisnessObjects.Project() With {.ProjectID = p.projectID, .ProjectName = p.projectName})***
Return returnList
End Function
End Class
Public Class Project
Implements IProject
Private _projectName As String
Property ProjectName() As String Implements IProject.ProjectName
Get
Return _projectName
End Get
Set(ByVal value As String)
_projectName = value
End Set
End Property
Private _projectID As Integer
Property ProjectID() As Integer Implements IProject.ProjectID
Get
Return _projectID
End Get
Set(ByVal value As Integer)
_projectID = value
End Set
End Property
End Class
Public Interface IProject
Property ProjectName() As String
Property ProjectID() As Integer
End Interface
I am having trouble with casting and interfaces. Below is the code I am using. What the error says. I dont fully understand what is going on. I thought that if i created a list of items which implement an interface, if i pass another item which also implements the interface then it should work. But it doesn't. Can someone explain why it doesn't and what I should do instead. Thank you very much
The error line has three astrix's to identify it. Again thank you very much
An unhandled exception of type
'System.InvalidCastException' occurred
in DAL.dllAdditional information: Unable to cast
object of type
'System.Data.Linq.DataQuery1[BuisnessObjects.Project]'
1[BuisnessObjects.IProject]'
to type
'System.Collections.Generic.IEnumerable
Imports BuisnessObjects
Public Class ProjectInfoRepository
Implements IProjectInfoRepository
Function GetAllProjects() As List(Of BuisnessObjects.IProject) Implements IProjectInfoRepository.GetAllProjects
Dim returnList As New List(Of BuisnessObjects.IProject)
***returnList.AddRange(From p In DC.ProjectInfos _
Select New BuisnessObjects.Project() With {.ProjectID = p.projectID, .ProjectName = p.projectName})***
Return returnList
End Function
End Class
Public Class Project
Implements IProject
Private _projectName As String
Property ProjectName() As String Implements IProject.ProjectName
Get
Return _projectName
End Get
Set(ByVal value As String)
_projectName = value
End Set
End Property
Private _projectID As Integer
Property ProjectID() As Integer Implements IProject.ProjectID
Get
Return _projectID
End Get
Set(ByVal value As Integer)
_projectID = value
End Set
End Property
End Class
Public Interface IProject
Property ProjectName() As String
Property ProjectID() As Integer
End Interface
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将
.Cast(Of BuisnessObjects.IProject)
添加到 linq 查询,它应该可以工作。If you add a
.Cast(Of BuisnessObjects.IProject)
to the linq query it should work.