VB LINQ查询Where子句*不起作用?

时间:2012-08-06 19:11:10

标签: vb.net linq wildcard where-clause

这是我用我的查询设置的功能,试图检索用户可能会或可能不会进入几个文本框的特定信息。用户可以输入...

  • 公司名称
  • 客户名字
  • techs名字

我想要的是,如果用户没有在文本框中放入任何内容,查询将返回所有字段,如“*”,我相信是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

1 个答案:

答案 0 :(得分:1)

  

我相信LINQ使用

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

如果你想处理“用户没有输入值”等情况,只需动态构建你的查询,这很容易,不要尝试使用通配符。毕竟,它会让你的搜索更快。