在Linq中喜欢SQL [英] LIKE in Linq to SQL
本文介绍了在Linq中喜欢SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个方法需要接受一组国家名称,并返回与这些国家名称之一匹配的记录列表.我正在尝试
I have a method that needs to accept an array of country names, and return a list of records that match one of those 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
但我收到此错误
*Only arguments that can be evaluated on the client are supported for the LIKE method
在普通的SQL中,这很简单:
In plain SQL, this would be simple:
Select ConcessionID,Title from Concessions c where @CountryList like '%' + c.Country + '%'
如何在Linq to SQL中实现此结果?
How can I achieve this result in Linq to SQL?
我得到了与string.Contains相同的消息.
I get the same message with string.Contains. It would be fine with
t.Country.contains(CountryList)
但我需要
CountryList.contains(t.Country)
并且抛出与我上面列出的相同的错误.
and that throws the same error I listed above.
推荐答案
我认为您要做的是从国家/地区构建列表并使用
I think what you want to do is construct a List from Countries and use
List<string> ListOfCountries = new List(Countries)
...ListOfCountries.Contains(t.Country)
这将转化为
t.Country IN ('yyy','zzz',...)
请原谅我的C#风格.
这篇关于在Linq中喜欢SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文