0

I have a listbox which users can select from a list if Towns, I want to be able to build a LINQ query based on the selected items in the list e.g.

Dim ddlTowns As ListBox = CType(Filter_Accommodation1.FindControl("ddlTowns"), ListBox)
        If Not ddlTowns Is Nothing Then
            For Each Item In ddlTowns.Items
                If Item.Selected Then
                    '// Build query
                End If
            Next
        End If

I have researched LinqKit as it appears to be able to do what I need but I cannot after hours of trying make any headway. I cannot find anything in VB which translates in anything meaningful or usable.

Shankar R10N
  • 4,926
  • 1
  • 21
  • 24
David
  • 11
  • 2

2 Answers2

1

Just had a Eureka moment and rather than using predicate I came up with this...

Private Function Filter_Accommomdation_QueryBuilder() As IEnumerable

            Dim ddlTowns As ListBox = CType(Filter_Accommodation1.FindControl("ddlTowns"), ListBox)
            Dim myList As New List(Of String)
            If Not ddlTowns Is Nothing Then
                For Each Item In ddlTowns.Items
                    If Item.Selected Then
                        myList.Add(Item.value)
                    End If
                Next
            End If

            Dim Filter_Query = _
                       From c In InitialQuery _
                       Where myList.ToArray.Contains(c.MyData.element("townvillage").value) _
                       Select c
            Return Filter_Query


        End Function

As a note I'm using c.MyData as the nature of InitialQuery demands a number of structured fields (the query is reused from various tables which by poor design aren't very consistant).

David
  • 11
  • 2
0

Check out this question - contains some useful VB examples for you: Using PredicateBuilder with VB.NET

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165