在 LINQ to SQL 中,您可以使用查询对象的 .Where 方法将 WHERE 子句添加到查询中,正如您在问题中所述。要使用 LIKE 运算符,请尝试在调用 Where 方法的 Lambda 表达式中使用您正在查询的对象的 .Contains 方法。
这是控制台应用程序中的一个简化示例。希望它将引导您朝着正确的方向前进。
Public Class Doc
Private _docName As String
Public Property DocName() As String
Get
Return _docName
End Get
Set(ByVal value As String)
_docName = value
End Set
End Property
Public Sub New(ByVal newDocName As String)
_docName = newDocName
End Sub
End Class
Sub Main()
Dim Documents As New List(Of Doc)
Documents.Add(New Doc("ABC"))
Documents.Add(New Doc("DEF"))
Documents.Add(New Doc("GHI"))
Documents.Add(New Doc("ABC DEF"))
Documents.Add(New Doc("DEF GHI"))
Documents.Add(New Doc("GHI LMN"))
Dim qry = From docs In Documents
qry = qry.Where(Function(d) d.DocName.Contains("GHI"))
Dim qryResults As List(Of Doc) = qry.ToList()
For Each d As Doc In qryResults
Console.WriteLine(d.DocName)
Next
End Sub
请注意 .Where 方法的 Lambda 表达式中的 .Contains("GHI") 调用。我引用了表达式的参数“d”,它公开了 DocName 属性,进一步公开了 .Contains 方法。这应该会产生您期望的 LIKE 查询。
此方法是附加的,即对 .Where 方法的调用可以包含在一个循环中,以便将其他 LIKE 运算符添加到查询的 WHERE 子句中。