“表达式类型”“不可查询”错误

时间:2015-08-04 16:00:03

标签: vb.net visual-studio-2013

我正在尝试为课程完成作业,并且遇到了这个错误:

  

错误1'Homework_Code_Examples.Form1.State'类型的表达式不可查询。确保您没有错过LINQ提供程序的程序集引用和/或名称空间导入。 D:\ Users \ Stryke \ Documents \ Visual Studio 2013 \ Projects \ Homework Code Examples \ Homework Code Examples \ Form1.vb 11 32作业代码示例

我之前从未遇到过这种情况,通过Google,MSDN和这些论坛进行搜索都告诉我,我必须为我的程序定位正确的.NET框架,但是我完全不知道我应该将哪个框架作为推荐目标答案都会在重新编译时给出相同的错误。目前,我在Visual Studio 2013中使用.NET Framework 4.5。

我不知道它是否有帮助,但这是我的代码,以便您可以看到我正在努力实现的目标......

Public Class Form1

    Dim states As State

    Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

        states = New State()

        Dim query = From st In states
                    Let name = states.Name
                    Let density = states.Density()
                    Order By density Descending
                    Select name, density

    End Sub

    Private Sub stateInfo_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles stateInfo.CellContentClick

    End Sub

    Class State

        Private m_name, m_abbr, m_date As String
        Private m_area, m_pop As Integer

        Public Property Name()
            Get
                Return m_name
            End Get
            Set(value)
                m_name = value
            End Set
        End Property
        Public Property Abbreviation()
            Get
                Return m_abbr
            End Get
            Set(value)
                m_abbr = value
            End Set
        End Property
        Public Property joinDate()
            Get
                Return m_date
            End Get
            Set(value)
                m_date = value
            End Set
        End Property
        Public Property landArea
            Get
                Return m_area
            End Get
            Set(value)
                m_area = value
            End Set
        End Property
        Public Property Population
            Get
                Return m_pop
            End Get
            Set(value)
                m_pop = value
            End Set
        End Property

        Public Function Density()
            Dim popDensity As Integer
            popDensity = Population / landArea
            Return popDensity
        End Function

    End Class

End Class

2 个答案:

答案 0 :(得分:3)

Linq需要遍历多个对象。在您的情况下,它可以是类的列表或集合。

列表(类型)

的示例

更改行

   Dim states As State

   Dim states As List(Of State)

    states = New State()

    states = New List(Of State)

数组

的示例

更改行

   Dim states As State

   Dim states() As State

    states = New State()

    ReDim states(10)   'or use a variable inside

答案 1 :(得分:1)

以下是如何填充数据的示例。我还修复了查询中的一些错误。将您的查询与我的查询进行比较,看看我发生了什么变化。

' initialize a list with some values
Dim states = New List(Of State) From {
    New State With {.Name = "Ohio", .Population = 10000, .landArea = 10},
    New State With {.Name = "Texas", .Population = 20000, .landArea = 300},
    New State With {.Name = "Florida", .Population = 5000, .landArea = 1000}
}

' query the list
Dim query = From st In states
        Let name = st.Name
        Let density = st.Density()
        Order By density Descending
        Select name, density

我还改进了你的State类,使用正确的数据类型而不是object。

Class State

    Private m_name, m_abbr, m_date As String
    Private m_area, m_pop As Integer

    Public Property Name As String
        Get
            Return m_name
        End Get
        Set (value As String)
            m_name = value
        End Set
    End Property
    Public Property Abbreviation As String
        Get
            Return m_abbr
        End Get
        Set(value As String)
            m_abbr = value
        End Set
    End Property
    Public Property joinDate As String
        Get
            Return m_date
        End Get
        Set(value As String)
            m_date = value
        End Set
    End Property
    Public Property landArea As Integer
        Get
            Return m_area
        End Get
        Set(value As Integer)
            m_area = value
        End Set
    End Property
    Public Property Population As Integer
        Get
            Return m_pop
        End Get
        Set(value As Integer)
            m_pop = value
        End Set
    End Property

    Public Function Density() As Double
        Dim popDensity As Double
        popDensity = Population / landArea
        Return popDensity
    End Function

End Class