如何使用 VB.NET 和 LINQ 附加“where”子句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/782566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I append a 'where' clause using VB.NET and LINQ?
提问by sugarcrum
I am pretty new to VB.NET and am having a bit of trouble here with something I thought should be simple.
我对 VB.NET 很陌生,在这里遇到了一些我认为应该很简单的问题。
Keeping it simple, let's say I have a Document table with "Name" that I want to search on (in reality there are several other tables, joins, etc. ..). I need to be able to build the query using a where
clause based on string values passed in.
保持简单,假设我有一个我想要搜索的带有“名称”的文档表(实际上还有其他几个表、连接等......)。我需要能够使用where
基于传入的字符串值的子句来构建查询。
Example - the user may pass in "ABC", "ABC DEF", "ABC DEF GHI".
示例 - 用户可以传入“ABC”、“ABC DEF”、“ABC DEF GHI”。
The final query would be (the syntax is not correct, I know):
最终查询将是(语法不正确,我知道):
Select * from Documents Where Name Like %ABC% AND Name Like %DEF% AND Name like %GHI%
So, I thought I could do something like this.
所以,我想我可以做这样的事情。
Dim query = From document In _context.Documents
<< loop based on number of strings passed in >>
query = query.Where( ... what goes here?? )
For some reason, being brain-dead or something, I can't figure out how to make this work in VB.NET, or if I'm doing it correctly.
出于某种原因,脑死亡或其他原因,我无法弄清楚如何在 VB.NET 中进行这项工作,或者我是否正确地进行了操作。
回答by Jimmie R. Houts
I believe this is how you would do it in VB (I'm a C# developer):
我相信这就是您在 VB 中的做法(我是 C# 开发人员):
query = query.where(Function(s) s = "ABC")
See LINQ - Sample Queriesfor some examples.
有关一些示例,请参阅LINQ - 示例查询。
回答by Jeremy Wiebe
I think the tricky part here is the unknown number of query parameters. You can use the underlying LINQ IQueryable(Of T) here to help.
我认为这里的棘手部分是未知数量的查询参数。您可以在此处使用底层 LINQ IQueryable(Of T) 来提供帮助。
I think the following would work (it's not compiled, just notepad code here):
我认为以下内容可以工作(它没有编译,这里只是记事本代码):
Public Function GetDocuments(criteria as String)
Dim splitCriteria = SplitTheCriteria(criteria)
dim query = from document in _context.Documents
For Each item in splitCriteria
Dim localItem = item
query = AddCriteriaToQuery(query, localItem)
Next
dim matchingDocuments = query.ToList()
End Function
Private Function AddCriteriaToQuery(query as IQueryable(Of Document), criteria as string) as IQueryable(Of Document)
return query.Where(Function(doc) doc.Name = criteria)
End Function
Since LINQ will delay-execute the query you can append where clauses onto your query in the loop and then call .ToList() at the end to execute the query.
由于 LINQ 会延迟执行查询,因此您可以在循环中将 where 子句附加到您的查询中,然后在最后调用 .ToList() 来执行查询。
回答by Bob Mc
In LINQ to SQL you can add WHERE clauses to your query using the .Where method of the query object, as you noted in your question. To use the LIKE operator, try using the .Contains method of the object you're querying in the Lambda expression of your call to the Where method.
在 LINQ to SQL 中,您可以使用查询对象的 .Where 方法将 WHERE 子句添加到您的查询中,如您在问题中所述。要使用 LIKE 运算符,请尝试使用您在调用 Where 方法的 Lambda 表达式中查询的对象的 .Contains 方法。
Here's a simplified example in a console application. Hopefully it will lead you in the correct direction.
这是控制台应用程序中的一个简化示例。希望它会引导您走向正确的方向。
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
Note the .Contains("GHI") call in the Lambda expression of the .Where method. I'm referencing the parameter of the expression, "d", which exposes the DocName property, which further exposes the .Contains method. This should produce the LIKE query you're expecting.
请注意 .Where 方法的 Lambda 表达式中的 .Contains("GHI") 调用。我引用了表达式的参数“d”,它公开了 DocName 属性,它进一步公开了 .Contains 方法。这应该会产生您期望的 LIKE 查询。
This method is additive, i.e. the call to the .Where method could be enclosed in a loop to make additional LIKE operators added to the WHERE clause of your query.
此方法是附加的,即对 .Where 方法的调用可以包含在循环中,以便将额外的 LIKE 运算符添加到查询的 WHERE 子句中。
回答by Sergio
Dim query = From document In _context.Documents where document.name = 'xpto' select document
Or
或者
Dim query = From document In _context.Documents where document.name.contains('xpto') select document
回答by TheCodeMonk
If you do this in a loop, you can do something like this:
如果您在循环中执行此操作,则可以执行以下操作:
.Where(Function(i as mytype) i.myfiltervar = WhatIWantToSelect)