LIKE in Linq to SQL

I have a method that should take an array of country names and return a list of entries that match one of these country names. I'm trying this

Public Shared Function GetConcessions(ByVal Countries As String()) As IEnumerable
    Dim CountryList As String = Utility.JoinArray(Countries) ' turns string array into comma-separated string
    Return (From t In New Db().Concessions _
                    Where CountryList Like t.Country _
                    Select t.ConcessionID, t.Title, t.Country)
End Function

but i get this error

  *Only arguments that can be evaluated on the client are supported for the LIKE method

In plain SQL, this would be simple:

 Select ConcessionID,Title from Concessions c where @CountryList like '%' + c.Country + '%'

How can I achieve this result in Linq to SQL?

Change (clarification)

I get the same message with a string. Contains. It would be nice with

t.Country.contains(CountryList)

but i need

CountryList.contains(t.Country) 

and this causes the same error as I mentioned above.

+3
source share
2 answers

I think you want to create a list from countries and use

List<string> ListOfCountries = new List(Countries)

...ListOfCountries.Contains(t.Country)

This will translate to

t.Country IN ('yyy','zzz',...)

Please excuse my C # -history.

+2
source

You can use SqlMethods.Like

eg.

Where SqlMethods.Like(t.country, "%Sweden%")
+9
source

Source: https://habr.com/ru/post/1698154/


All Articles