简体   繁体   English

在LINQ中使用SQL通配符

[英]Using SQL Wildcards with LINQ

I have a question about using LINQ to access a Database and trying to make use of it's version of accessing the LIKE comparison operator. 我对使用LINQ访问数据库并尝试利用其访问LIKE比较运算符的版本有疑问。

I know that LINQ has .Contains() , .StartsWith() , and .EndsWith() as comparison methods. 我知道LINQ具有.Contains() .StartsWith().EndsWith()作为比较方法。 However I am wondering if there is a way to programatically imcorporate SQL Wildcards into a LINQ statement without explicitly using these query operators. 但是我想知道是否有一种方法可以在不显式使用这些查询运算符的情况下以编程方式将SQL通配符合并到LINQ语句中。 Let me explain my situation. 让我解释一下我的情况。

I am writing a program that accesses a database, and part of the program is a search window which the user can use to help them find specific database data. 我正在编写一个访问数据库的程序,该程序的一部分是搜索窗口,用户可以使用该窗口来帮助他们找到特定的数据库数据。 I would like to try and incorporate SQL Wildcards into the textbox fields for these search pages. 我想尝试将SQL通配符合并到这些搜索页面的文本框字段中。

For example if a user enters the input 17% I'd want the program to check for anything in that specific column that starts with a 17 . 例如,如果用户输入17%的输入,我希望程序检查以17开头的特定列中的所有内容。 The same is true with %17 and 17 where I'd want it to search for columns that end with, and contain the values. 对于%1717同样如此,我希望它搜索以结尾并包含值的列。

Currently, this is the code I have for my search method: 目前,这是我用于搜索方法的代码:

Public Function Data_Search(sData As List(Of String), DB As CustomDataContext) As DataGridView
    Dim gridData As New DataGridView
    Dim query = From p In DB.Parts
                Select p.Part_Number, p.Part_Description, p.Supplier.Supplier_Name
    for i = 0 To sData.Count - 1
        If Not sData(i).ToString() = "" Then
            Select Case i
                Case 0
                    Dim partNum As String = sData(i).ToString()
                    query = query.Where(Function(x) x.Part_Number.Contains(partNum))
                Case 1
                    Dim description As String = sData(i).ToString()
                    query = query.Where(Function(x) x.Part_Description.Contains(description))
                Case 2
                    Dim supp As String = sData(i).ToString()
                    query = query.Where(Function(x) x.Supplier_Name.Contains(supp))
            End Select
        End If
    Next
    gridData.DataSource = query.ToList()
    Return gridData
End Function

So right now I am trying to see if there is a way for me to modify the code in a way that doesn't essentially involve me putting a substring search into each Case section to determine if I should be using StartsWith() , Contains() , or EndsWith . 因此,现在我正在尝试查看是否有一种方法可以使我以基本上不涉及在每个Case部分中进行子字符串搜索的方式来修改代码,以确定是否应使用StartsWith()Contains()EndsWith

Thanks for your time. 谢谢你的时间。

如果您正在使用LINQ to SQL,并且正在与Microsoft SQL Server通讯,则可以使用SQLMethods.Like来实现SQL LIKE

query = query.Where(Function(x) SQLMethods.Like(x.Part_Number, partNum))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM