与 LINQ to Entity Framework 匹配的复杂字符串

发布于 2024-08-08 13:56:00 字数 563 浏览 4 评论 0原文

我们正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱她像谁 2024-08-15 13:56:00

不要使用 Equals,而是尝试使用 Contains,这应该会带上您的通配符,因为当您使用 Contains 时,LINQ 内部会使用 LIKE

var results = from a in mycontainer.users
              where a.fullname.Contains(newString)
              select a.fullname;

instead of using Equals try using Contains this should take your wild cards because internally LINQ uses LIKE when you use Contains

var results = from a in mycontainer.users
              where a.fullname.Contains(newString)
              select a.fullname;
紙鸢 2024-08-15 13:56:00

有一篇很棒的文章描述了如何做到这一点:
实体框架中的正则表达式

There's a great article that describes how to do it:
Regex in entity framework

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文