与LINQ to Entity Framework匹配的复杂字符串
-
05-07-2019 - |
题
我们正在使用LINQ to EF开发解决方案。
在我的LINQ查询中,我想要
string toMatch = "Matt Cool";
newString = toMatch.Replace(" ", "% ") + "%";
// So here newString is 'Matt% Cool%'
/*Now in my datasource, there is no 'Matt Cool', there's 'Matthew Coolguy'.
I would like this query to return that result.
I would expect this behavior with the
wildcard. It doesn't work*/
var results = from a in mycontainer.users
where a.fullname.equals(newString)
select a.fullname;
我试过“*”作为通配符和正则表达式解决方案,无济于事 - 还有其他选择吗?
解决方案
而不是使用等于尝试使用包含这应该使用您的外卡,因为当您使用包含<时内部LINQ使用 LIKE /强>
var results = from a in mycontainer.users
where a.fullname.Contains(newString)
select a.fullname;
其他提示
有一篇很棒的文章描述了如何做到这一点: 实体框架中的正则表达式
不隶属于 StackOverflow