views:

6281

answers:

5
A: 

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

Sergio
A: 

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.

Jimmie R. Houts
VB's comparison operator is the same as its assignment operator.. so 's == "ABC"' should be '("ABC" = s)'. Other than that, good job.
Boo
A: 

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

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

TheCodeMonk
+2  A: 

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.

Jeremy Wiebe
+1  A: 

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.

Bob Mc