vb 与 linq-to-sql 的接口

发布于 2024-10-20 02:10:28 字数 1684 浏览 1 评论 0原文

我在转换和界面方面遇到问题。下面是我正在使用的代码。错误说了什么。我不完全明白发生了什么事。我认为,如果我创建了一个实现接口的项目列表,如果我传递另一个也实现该接口的项目,那么它应该可以工作。但事实并非如此。有人可以解释为什么不这样做以及我应该做什么。非常感谢

错误行有三个 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.dll

Additional information: Unable to cast
object of type
'System.Data.Linq.DataQuery1[BuisnessObjects.Project]'
to type
'System.Collections.Generic.IEnumerable
1[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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

他夏了夏天 2024-10-27 02:10:28

如果将 .Cast(Of BuisnessObjects.IProject) 添加到 linq 查询,它应该可以工作。

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}).Cast(Of BuisnessObjects.IProject))

        Return returnList
End Function

If you add a .Cast(Of BuisnessObjects.IProject) to the linq query it should work.

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}).Cast(Of BuisnessObjects.IProject))

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