我有一个方法需要接受一个国家/地区名称数组,并返回与其中一个国家/地区名称匹配的记录列表.我正在尝试这个
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
但是我得到了这个错误
*Only arguments that can be evaluated on the client are supported for the LIKE method
在纯SQL中,这很简单:
Select ConcessionID,Title from Concessions c where @CountryList like '%' + c.Country + '%'
如何在Linq to SQL中实现此结果?
我用string.Contains得到了相同的消息.没关系
t.Country.contains(CountryList)
但是我需要
CountryList.contains(t.Country)
并抛出我上面列出的相同错误.
您可以使用SqlMethods.Like
例如
Where SqlMethods.Like(t.country, "%Sweden%")