Dynamic where clause query (elements of concatenated array)

I have a dynamic array where the items have to filter items (where clause) in a linq query.

I am using the library System.Linq.Dynamic

mentioned in this question . If I run:

Dim query = From element In dtImitate.Where("Team = ""Avangers"" Or Team = ""Asgard""")

      

the request works. But if I collect the elements of the array and put them in a string like this:

Public Shared Function CreateDynString(ByRef arr As String()) As String
 Dim a As New StringBuilder
        a.Append("""")
        For Each element In arr
            a.Append("Team = ").Append("""""").Append(element).Append("""""").Append(" or ")
        Next

        Dim b As Integer = a.Length - 4
        a.Remove(b, 4)
        a.Append("""")
        Return a.ToString
End Function

      

and run the query:

Dim s As String = DynamicStringBuilder.CreateDynString(teamsArray).ToString
Dim query = From element In dtImitate.Where(s)

      

Nothing happens. My DGV remains empty. Can anyone help me get this query to work and can tell me why it is not working. If I type s

this "Team = ""Avangers"" Or Team = ""Asgard"""

. I don't know why it doesn't work.

+3


source to share


1 answer


If you are checking that a property is in an array, you can use the extension method Contains

on IEnumerable

, which is probably preferred for string construction. Try the following:



Dim query = From element In dtImitate.Where(function (el) teamsArray.Contains(el.Team))

      

+3


source







All Articles