0

有谁知道我为什么会出现这个错误?

我收到以下错误:

你调用的对象是空的

这是有问题的LINQ。感谢您提前提供任何帮助。

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
4

2 回答 2

1

如果将第一行更改为:

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

这表明座位列表中有一个空值,Where上面的 lambda 应该删除它们。

于 2012-04-15T16:04:36.437 回答
0

你的 Where 子句对我来说看起来很奇怪。您不应该使用 = 运算符,而不是 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

你确定每一行都用空格而不是其他东西分隔吗?即使有 Is 也不应该导致 Null Ref。

这是我的示例程序,它似乎可以工作(在 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
于 2012-04-16T17:42:23.860 回答