简体   繁体   English

Visual Basic LINQ错误

[英]Visual basic LINQ error

Anyone know why I am having this error? 有人知道我为什么会有这个错误吗?

I am getting the following error: 我收到以下错误:

Object reference not set to an instance of an object 你调用的对象是空的

Here is the LINQ in question. 这是有问题的LINQ。 Thank you for any help in advance. 感谢您的任何帮助。

Dim q = From line In seats
        Let data = line.Split(New [Char]() {" "c}, StringSplitOptions.RemoveEmptyEntries) ' it occurs in this line '
        Let seatA = data(0)
        Let seatB = data(1)
        Let seatC = data(2)
        Let seatD = data(3)
        Let seatE = data(4)
        Let seatF = data(5)
        Where seatA Is "."
        Where seatB Is "."
        Select seatA, seatF

Does it work if you change the first line to: 如果将第一行更改为:

Dim windowSeatQuery = From line In seats.Where(function (s) s isnot nothing)

This would indicate that there a null values in the seats list, and the Where lambda above should remove them. 这表示席位列表中存在空值,并且上面的Where lambda应该将其删除。

Your Where clauses look strange to me. 您的Where子句对我来说很奇怪。 Shouldn't you be using the = operator, not Is: 您不应该使用=运算符,而不是:

Dim q = From line In seats
    Let data = line.Split(New [Char]() {" "c}, StringSplitOptions.RemoveEmptyEntries) ' it occurs in this line '
    Let seatA = data(0)
    Let seatB = data(1)
    Let seatC = data(2)
    Let seatD = data(3)
    Let seatE = data(4)
    Let seatF = data(5)
Where seatA = "." AndAlso seatB = "."   '<------   Use = on this line not Is
Select seatA, seatF

And are you sure that each line is delimited with spaces instead of something else? 并且您确定每一行都用空格而不是其他空格分隔吗? Even having Is in there should not result in a Null Ref. 即使其中存在Is也不会导致Null Ref。

Here's my example program that seems to work (tested in LinqPad which doesn't seem to support implicit line continuation characters): 这是我的示例程序,似乎有效(在LinqPad中测试,该程序似乎不支持隐式的行连续字符):

Sub Main
    Dim seats As New List(Of String)()
    seats.Add("1 2 3 4 5 6")
    seats.Add(". . 9 8 7 6")
    seats.Add(". . 5 5 5 5")
    seats.Add("1 2 3 4 5 6")
    seats.Add(". . 2 4 6 8")
    seats.Add("1 2 3 4 5 6")

    Dim q = From line In seats _
        Let data = line.Split(New [Char]() {" "c}, StringSplitOptions.RemoveEmptyEntries) _
        Let seatA = data(0) _
        Let seatB = data(1) _
        Let seatC = data(2) _
        Let seatD = data(3) _
        Let seatE = data(4) _
        Let seatF = data(5) _
    Where seatA = "." AndAlso seatB = "." _
    Select seatA, seatF

    q.Dump()
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM