我需要使用 LINQ 返回与提供的标识字段匹配的客户端列表,这些字段位于单独的表中。如果 SQL 语句位于存储过程中,则该 SQL 语句将如下所示:
Select client.*
From ADLT_Clients as client
Inner Join ADLT_ClientIdentifiers as identifier
on client.ClientPIN = identifier.ClientPIN
Where identifier.EmailAdrs = @EmailAddress
and identifier.FBINum = @FBINumber
(...)
我正在尝试使用以下方法在 LINQ 中执行此操作:
Public Function FindByIndentifiers(adultClient As AdultClient) As List(Of AdultClient)
Dim adultClients As New List(Of AdultClient)
Dim clients = From client As ADLT_Client In _cmisiiEntities.ADLT_Clients
Join identifier In _cmisiiEntities.ADLT_ClientIdentifiers On client.ClientPIN Equals identifier.ClientPIN
Select client
If adultClient.AdultClientIdentifiers IsNot Nothing Then
With adultClient.AdultClientIdentifiers
If Not String.IsNullOrEmpty(.EmailAddress) Then clients = clients.Where(Function(f) f.ADLT_ClientIdentifiers.EMailAdrs.Contains(.EmailAddress))
If Not String.IsNullOrEmpty(.FBINumber) Then clients = clients.Where(Function(f) f.ADLT_ClientIdentifiers.FBINum.Contains(.FBINumber))
If Not String.IsNullOrEmpty(.ICOTSNumber) Then clients = clients.Where(Function(f) f.ADLT_ClientIdentifiers.ICOTSNum.Contains(.ICOTSNumber))
If Not String.IsNullOrEmpty(.InmateNumber) Then clients = clients.Where(Function(f) f.ADLT_ClientIdentifiers.InmateNum.Contains(.InmateNumber))
If Not String.IsNullOrEmpty(.LicenseNumber) Then clients = clients.Where(Function(f) f.ADLT_ClientIdentifiers.LicenseNum.Contains(.LicenseNumber))
If Not String.IsNullOrEmpty(.LicenseNumberState) Then clients = clients.Where(Function(f) f.ADLT_ClientIdentifiers.LicenseNumState.Contains(.LicenseNumberState))
If Not String.IsNullOrEmpty(.PassportNumber) Then clients = clients.Where(Function(f) f.ADLT_ClientIdentifiers.PassportNum.Contains(.PassportNumber))
If Not String.IsNullOrEmpty(.AlienRegistrationNumber) Then clients = clients.Where(Function(f) f.ADLT_ClientIdentifiers.GreenCardNum.Contains(.AlienRegistrationNumber))
If Not String.IsNullOrEmpty(.SocialSecurityNumber) Then clients = clients.Where(Function(f) f.ADLT_ClientIdentifiers.SSN.Contains(.SocialSecurityNumber))
If Not String.IsNullOrEmpty(.SPBINumber) Then clients = clients.Where(Function(f) f.ADLT_ClientIdentifiers.SPBINum.Contains(.SPBINumber))
End With
End If
For Each client In clients.Take(100).ToList()
adultClients.Add(BuildAdultClient(client))
Next
Return adultClients
End Function
当此代码在没有有效参数的情况下执行时,不会添加任何 where 子句,并且 clients.Take(100).ToList() 语句可以正常执行。如果我包含任何有效参数,从而添加 where 子句,则该语句会在 ToList() 调用上给出 NullReferenceException。
我的理论是我要添加的 where 子句存在别名问题。当 where 子句是初始语句的一部分时,我使用由 join 语句创建的标识符别名。如果我将我的陈述修改为:
Dim clients = (From client As ADLT_Client In _cmisiiEntities.ADLT_Clients
Join identifier In _cmisiiEntities.ADLT_ClientIdentifiers On client.ClientPIN Equals identifier.ClientPIN) _
.Where(Function(f) f.identifier.EMailAdrs.Contains(adultClient.EmailAddress)) _
.Select(Function(f) f.client)
代码也将执行。这里的区别是我正在通过定义的标识符别名。我不能在条件 where 语句中执行相同的 f.identifier.EmailAdrs 因为标识符不是该上下文的一部分?
我在这里遗漏了什么,有没有更好的方法来正确地做到这一点,或者这是 VB 的限制?