2012-11-29 131 views
2

我需要返回一個客戶端列表,它們與使用LINQ的獨立表中提供的標識字段相匹配。該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的限制嗎?

+0

我不得不懷疑這是一個VB問題與表達式有問題在你身邊使用'With'子句 – Maslow

回答

0

如果我這樣做是linqPad,與LINQ2SQL

Dim c as new Config with {.Category="Other"} 
Dim q =from c1 in Configs select c1 

if(c isnot nothing) then 
    if not string.isnullorempty(c.Category) then q=q.Where(Function(f) f.Category.Contains(c.Category)) 
end if 
q.Dump() 

它的工作原理,但如果我這樣做

Dim c as new Config with {.Category="Other"} 
Dim q =from c1 in Configs select c1 

if(c isnot nothing) then 
    with c 
     if not string.isnullorempty(.Category) then q=q.Where(Function(f) f.Category.Contains(.Category)) 
    end with 
end if 
q.Dump() 

它拋出一個空引用異常。我認爲這是你的子句與表達式衝突

+0

從我的代碼中刪除「With adultClient.AdultClientIdentifiers」解決了它。謝謝 – Meshed

相關問題