与 LINQ to Entity Framework 匹配的复杂字符串
我们正在使用 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;
尝试“*”作为通配符和正则表达式解决方案,但无济于事 - 还有其他选择吗?
We are using LINQ to EF to develop a solution.
In my LINQ query I would like
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;
I tried "*" as a wildcard and a regex solution, to no avail -- Are there any other options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用 Equals,而是尝试使用 Contains,这应该会带上您的通配符,因为当您使用 Contains 时,LINQ 内部会使用 LIKE
instead of using Equals try using Contains this should take your wild cards because internally LINQ uses LIKE when you use Contains
有一篇很棒的文章描述了如何做到这一点:
实体框架中的正则表达式
There's a great article that describes how to do it:
Regex in entity framework