2012-08-06 106 views
-1

這是我設置的一個函數,用於查詢用戶可能會或可能不會進入幾個文本框的特定信息。用戶可以把一個...VB LINQ查詢where子句*不工作?

  • 公司名稱
  • 客戶先命名
  • 科技股先命名

我要的是,如果用戶不把任何東西在文本框中該查詢將返回所有字段,如「*」我相信是LINQ使用和SQL使用「%」。我已經試過並且無法弄清楚爲什麼我無法找到所有客戶和所有技術人員的某家公司。

Public Function SpecificQueryInvoices(ByVal Company As String, ByVal Clientname As String, ByVal techname As String) 

    Dim specificQuerySetInformation = From invo In database.Invoices 
           Join orgs In database.Organizations 
           On orgs.OrgId Equals invo.OrgId 
           Join clien In database.Clients 
           On clien.ClientId Equals invo.ClientId 
           Join tec In database.Teches 
           On tec.TechId Equals invo.TechId 
           Order By invo.InvoiceId 
           Where orgs.OrgName.StartsWith(Company) And clien.FirstName.StartsWith(Clientname) And tec.FirstName.StartsWith(techname) 
           Select New With {.Company = orgs.OrgName, .Client = clien.FirstName, _ 
               .Tech = tec.FirstName, invo.Date, _ 
           invo.Notes, invo.Parts, invo.Labor, invo.Mileage, invo.TotalCost, _ 
           invo.InvoiceNumber} 


    Return specificQuerySetInformation 

End Function 

    Private Sub btnFilter_Click(sender As System.Object, e As System.EventArgs) Handles btnFilter.Click 

    Dim companyName As String 
    Dim ClientFirstName As String 
    Dim TechFirstName As String 



    If txtCompany.Text = "" Then 
     companyName = "*" 
    Else 
     companyName = txtCompany.Text 
    End If 

    If txtClientName.Text = "" Then 
     ClientFirstName = "*" 
    Else 
     ClientFirstName = txtClientName.Text 
    End If 

    If txtTechName.Text = "" Then 
     TechFirstName = "*" 
    Else 
     TechFirstName = txtTechName.Text 
    End If 


    DataGridView2.DataSource = SpecificQueryInvoices(companyName, ClientFirstName, TechFirstName) 


    MsgBox("Filter requested") 
End Sub 
+0

這段代碼如何編譯,你的函數聲明甚至沒有指定返回類型? – 2012-08-07 11:41:02

回答

1

我相信就是LINQ使用

你錯了。 LINQ根本不使用通配符。

如果你想處理像「用戶沒有輸入值」的情況,只需動態構建你的查詢,這很容易,而且不要嘗試使用通配符。畢竟,它會讓你的搜索更快。