【问题标题】:Complex string matching with LINQ to Entity Framework与 LINQ to Entity Framework 匹配的复杂字符串
【发布时间】:2009-10-22 21:23:41
【问题描述】:

我们正在使用 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;

我尝试将“*”作为通配符和正则表达式解决方案,但无济于事——还有其他选择吗?

【问题讨论】:

  • 我认为您可以将代码的第一部分替换为 newString = toMatch.Replace(" ", "% ") + "%";
  • 是的,你是对的,我认为出于某种原因我的显然更明显...... :-)

标签: c# .net entity-framework linq-to-entities wildcard


【解决方案1】:

而不是使用 Equals 尝试使用 Contains 这应该使用您的通配符,因为当您使用 ContainsLIKE /strong>

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

【讨论】:

    【解决方案2】:

    有一篇很棒的文章描述了如何做到这一点: Regex in entity framework

    【讨论】:

    • this page 上好像有一篇破文章的副本。
    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 2014-06-19
    • 2011-01-20
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多