How do I append a 'where' clause using VB.NET and LINQ?

前端 未结 5 1331
别跟我提以往
别跟我提以往 2020-12-16 10:28

I am pretty new to VB.NET and am having a bit of trouble here with something I thought should be simple.

Keeping it simple, let\'s say I have a Document table with \

相关标签:
5条回答
  • 2020-12-16 10:58
    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 
    
    0 讨论(0)
  • 2020-12-16 11:04

    If you do this in a loop, you can do something like this:

    .Where(Function(i as mytype) i.myfiltervar = WhatIWantToSelect)
    
    0 讨论(0)
  • 2020-12-16 11:05

    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.

    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.

    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.

    0 讨论(0)
  • 2020-12-16 11:14

    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.

    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.

    0 讨论(0)
  • 2020-12-16 11:16

    I believe this is how you would do it in VB (I'm a C# developer):

    query = query.where(Function(s) s = "ABC")
    

    See LINQ - Sample Queries for some examples.

    0 讨论(0)
提交回复
热议问题