函数和LINQ查询无法正常工作

时间:2017-02-04 01:06:57

标签: arrays vb.net linq listbox

Public Class Records
    Dim Items() As String = IO.File.ReadAllLines("Inventory.txt")
    Dim Parts(7) As String

    Private Sub Records_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        lstOuput.Items.Clear()
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        End
    End Sub

    Private Sub btnFullList_Click(sender As Object, e As EventArgs) Handles btnFullList.Click
        Dim x As String = ""
        Dim TheFullList = From Thing In Items
                          Let Parts = Thing.Split(" "c)
                          Let TheRoom = RoomName(x)
                          Let ItemNumber = Parts(0)
                          Let ItemName = Parts(1)
                          Let PurchaseDate = Parts(3)
                          Let PurchasePrice = Parts(4)
                          Let Result = ItemNumber & " " & ItemName & " " & " " & PurchaseDate & " " & PurchasePrice
                          Order By ItemNumber
                          Select Result

        For Each Thing In TheFullList
            lstOuput.Items.Add(Thing)
            lstOuput.Items.Add(" ")
            lstOuput.Items.Add(RoomName(x))
        Next
    End Sub
    Function RoomName(x As String) As String
        Dim TheOutput As String = ""
        Dim GetRoomQuery = From TheRoom In Items
                           Let Parts = TheRoom.Split(" "c)
                           Let ActualName = Parts(0)
                           Let TrimmedName = Parts(0).Substring(0, 2)
                           Select TrimmedName
        If GetRoomQuery(0) = "LVR" Then
            TheOutput = "Living Room"
        End If
        Return TheOutput
    End Function

    Private Sub btnFindItem_Click(sender As Object, e As EventArgs) Handles btnFindItem.Click

    End Sub

    Private Sub btnID_Click(sender As Object, e As EventArgs) Handles btnID.Click

    End Sub

    Private Sub btnDepreciation_Click(sender As Object, e As EventArgs) Handles btnDepreciation.Click

    End Sub

    Private Sub btnFindYear_Click(sender As Object, e As EventArgs) Handles btnFindYear.Click

    End Sub
End Class

数组Items()是通过读取文本文件名“Inventory.txt”形成的。 我想要做的是从btnFullList事件过程中获取LINQ查询的“Item Number”部分。然后我想获取该信息并输入一个函数,该函数将查看前三个字符并使用IF块来确定正确的输出。

现在的问题是没有返回任何内容,单击按钮后出现NullException错误。

另外,在btnFullList过程LINQ查询中,结果中有更多的部分,但是我删除了它们。点击按钮后,即使在编辑了数组读取的文本文件后,我仍然会显示删除的结果。

1 个答案:

答案 0 :(得分:0)

这是你真正想要的循环。你的LINQ输出应该是循环的集合。

For Each Thing In TheFullList

            lstOuput.Items.Add(Thing)
            lstOuput.Items.Add(" ")
            lstOuput.Items.Add(RoomName(x))


        Next

您应该注意的另一件事是您可以将LINQ输出简化为这样的列表。

Dim lstOutput As List(Of Object) = TheFullList.ToList()

您的RoomName功能中也有一些错误。请参阅下面的评论。

Function RoomName(x As String) As String
        Dim TheOutput As String
        Dim GetRoomQuery = From TheRoom In Items
                           Let Parts = TheRoom.Split(" "c)
                           Let ActualName = Parts(0)
                           'I changed this to be length of 3 to match what you are doing in the IF
                           Let TrimmedName = Parts(0).Substring(0, 3)
                           Select TrimmedName

        'Here i am using the output of the LINQ
        If GetRoomQuery  = "LVR" Then
            TheOutput = "Living Room"
        End If
        Return TheOutput
    End Function