开发者

Complex string matching with LINQ to Entity Framework

开发者 https://www.devze.com 2022-12-09 09:40 出处:网络
We are using LINQ to EF to develop a solution. In my LINQ query I would like string toMatch = \"Matt Cool\";

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开发者_StackOverflow社区?


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;


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号