Sunday, May 1, 2011

VB.Net Linq - How to append a where clause?

Pretty new to VB.Net. Having a bit of trouble here with something I though should be simple.

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 where clause based on string values passed in.

Example - user may pass in "ABC" or "ABC DEF" or "ABC DEF GHI".

The final query would be (syntax not correct i know)

Select * from Documents Where Name Like %ABC% AND Name Like %DEF% AND Name like %GHI%

So, I though 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.

From stackoverflow
  • 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

  • I believe this is how you would do it in VB (I'm a c# dev)

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

    See 101 Linq Samples for some examples.

    Boo : VB's comparison operator is the same as its assignment operator.. so 's == "ABC"' should be '("ABC" = s)'. Other than that, good job.
  • If you do this in a loop, you can do something like this:

    .Where(Function(i as mytype) i.myfiltervar = WhatIWantToSelect)

  • 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.

  • 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 comments:

Post a Comment