为什么我得到 Expression Expected?

Why am I getting Expression Expected?

这是我的简单 CodeFile vb 代码,用于用户名和密码登录表单,重定向到不同的 'members area' 页面:

Public Class MyPage
Inherits Page
Private Structure Cred
    Public Username As String
    Public Password As String
    Public RedirectUrl As String
    Public Sub New(un As String, pw As String, Optional ru As String = "/admin/default.aspx")
        Username = un
        Password = pw
        RedirectUrl = ru
    End Sub
End Structure

Private ReadOnly _credentials As System.Collections.Generic.IEnumerable(Of Cred) =  New Cred(){New Cred("userone", "passwordone"), New Cred("usertwo", "passwordtwo"), New Cred("userthree", "passwordthree", "/admin/custom.aspx")}

Public Sub Page_Load(sender As Object, e As EventArgs)
    Dim user = _credentials.SingleOrDefault(Function(x) x.Username = UserName.Text AndAlso x.Password = Password.Text)
    If user IsNot Nothing Then
        Session("Admin") = True
        Response.Redirect(user.RedirectUrl)
    Else
        Session("Admin") = False
        LtlLogin.Text = "<p>Sorry, you have provided incorrect login details.</p>"
    End If
End Sub
End Class

在线:

Dim user = _credentials.SingleOrDefault(Function(x) x.Username = UserName.Text AndAlso x.Password = Password.Text)

非常感谢。

大卫.

问题是您在 Cred 中使用 structure 而不是 class。 请注意,结构是值类型,classes 是引用类型。

所以:

Dim user = _credentials.SingleOrDefault(Function(x) x.Username = UserName.Text AndAlso x.Password = Password.Text)

总是return一个结构(当没有找到时,结构的成员将获得它们的默认值)。

您不能将结构与 Nothing 进行比较,因为 ti 不是引用类型。

将结构更改为 class,你会没事的。

或更改支票:

If Not user.Equals(New Cred) Then

勾选this

更新示例

Class信誉

Imports System.Linq

Module StartupModule

    Private ReadOnly _credentials As System.Collections.Generic.IEnumerable(Of Cred) = New Cred() {
        New Cred("userone", "passwordone"),
        New Cred("usertwo", "passwordtwo"),
        New Cred("userthree", "passwordthree", "/admin/custom.aspx")}

    Sub Main()
        Dim userName As String = ""
        Dim password As String = ""

        Dim crd = _credentials.Where(Function(x) x.Username = userName AndAlso x.Password = password).SingleOrDefault

        If crd Is Nothing Then
            Console.WriteLine("user is nothing")
        Else
            Console.WriteLine("user is something")
        End If


        Console.ReadLine()
    End Sub

    Private Class Cred
        Public Username As String
        Public Password As String
        Public RedirectUrl As String
        Public Sub New(un As String, pw As String, Optional ru As String = "/admin/default.aspx")
            Username = un
            Password = pw
            RedirectUrl = ru
        End Sub
    End Class


End Module

结构信誉

Imports System.Linq

Module StartupModule

    Private ReadOnly _credentials As System.Collections.Generic.IEnumerable(Of Cred) = New Cred() {
        New Cred("userone", "passwordone"),
        New Cred("usertwo", "passwordtwo"),
        New Cred("userthree", "passwordthree", "/admin/custom.aspx")}

    Sub Main()
        Dim userName As String = ""
        Dim password As String = ""

        Dim crd = _credentials.Where(Function(x) x.Username = userName AndAlso x.Password = password).SingleOrDefault

        If crd.Equals(New Cred) Then
            Console.WriteLine("user is nothing")
        Else
            Console.WriteLine("user is something")
        End If


        Console.ReadLine()
    End Sub

    Private Structure Cred
        Public Username As String
        Public Password As String
        Public RedirectUrl As String
        Public Sub New(un As String, pw As String, Optional ru As String = "/admin/default.aspx")
            Username = un
            Password = pw
            RedirectUrl = ru
        End Sub
    End Structure


End Module