使用SQL LIKE运算符的LINQ to Entities [英] LINQ to Entities using the SQL LIKE operator
本文介绍了使用SQL LIKE运算符的LINQ to Entities的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个:
query = query.Where(s => s.ShowTypeDescription == showTypeDescription);
多次不同的变量以构建动态SQL.
several times for different variables in order to build dynamic SQL.
我该如何改变上面的说法:
How would I go about transforming the above to say:
query = query.Where(s => s.ShowTypeDescription **LIKE** showTypeDescription);
?
推荐答案
如果只想在另一个字符串中找到一个子字符串,最好的方法是使用Contains
方法:
If all you want is to find a substring within another string, the best way to do this is with the Contains
method:
query = query.Where(s => s.ShowTypeDescription.Contains(showTypeDescription));
因为String.Contains
方法转换为:
CHARINDEX(ShowTypeDescription, @showTypeDescription) > 0
大致相当于:
ShowTypeDescription LIKE '%' + @showTypeDescription + '%'
Update: In Linq-to-SQL, you can use the SqlMethods.Like
method:
query = query.Where(s => SqlMethods.Like(s.ShowTypeDescription, showTypeDescription));
这将直接转换为SQL LIKE
运算符.但是请注意,这在Linq-to-SQL查询之外不起作用.尝试在其他上下文中调用此方法将引发异常.
This will directly translate to the SQL LIKE
operator. Note, however, this won't work outside of Linq-to-SQL queries. Trying to call this method in other contexts will throw an exception.
这篇关于使用SQL LIKE运算符的LINQ to Entities的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文